Depending on exactly what you want there are two main ways (with
several different ways of expressing the most common):
if x.instance_of? Array … # tests if x is an Array (strictly)
if x.kind_of? Array … # test if x is an instance of Array or one of
its descendants
if x.is_a? Array … # same as kind_of?
if Array === x … # same as kind_of?, useful to know exists because
it lets you match against classes in case statements
Yes. “when” uses the === method of the object given, and Array is a
constant instance of class Class, and Class#===(obj) (inherited from
Module) is equivalent to obj.kind_of?(self)
is
if x == Array
also a valid statement??
x == Array is a valid statement, but it doesn’t mean the same thing.
case x
when Array: …
end
is the same as:
if Array === x then … end
not:
if x == Array then … end
i was expecting having to use something like x.is_a as Stefano said.
As is oft the case with Ruby, there is more than one way to do it.