Ruby - test for array

BuddyJoe picture BuddyJoe · Oct 6, 2009 · Viewed 224.1k times · Source

What is the right way to:

is_array("something") # => false         (or 1)

is_array(["something", "else"]) # => true  (or > 1)

or to get the count of items in it?

Answer

ry. picture ry. · Oct 6, 2009

You probably want to use kind_of().

>> s = "something"
=> "something"
>> s.kind_of?(Array)
=> false
>> s = ["something", "else"]
=> ["something", "else"]
>> s.kind_of?(Array)
=> true