Ruby - test for array
Solution 1:
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
Solution 2:
Are you sure it needs to be an array? You may be able to use respond_to?(method)
so your code would work for similar things that aren't necessarily arrays (maybe some other enumberable thing). If you do actually need an array
, then the post describing the Array#kind\_of?
method is best.
['hello'].respond_to?('each')