Saturday, July 3, 2010

Ruby on Rails: Array : Nil item

@array = [ 1, nil, 3, nil, 5 ]

Remove nil item

@array.compact    #=> [ 1, 3, 5 ]


Count total of nil item

@array.length    #=> 5
@array.nitems    #=> 3
total_nil = @array.length - @array.nitems


Check the present of nil item

@array.include?nil    #=> true


Location of the nil item

@array.index(nil)    #=> 1



Refer site: http://ruby-doc.org/core/classes/Array.html


If you find this useful, would you like to buy me a drink? No matter more or less, it will be an encouragement for me to go further. Thanks in advance!! =)

3 comments: