Christopher Dicely
2/9/2008 10:26:00 PM
On Feb 9, 2008 2:14 PM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> Farrel Lifson wrote:
>
> > Use a case statement:
> >
> > array.each do |x|
> > case x
> > when Array: do something
> > when Integer: do something
> > end
> > end
>
> is
>
> when Array: do something
>
> a valid statement??
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.