case statements use the '===' operator. the '===' for classes is the same as
'is_a?' for classes. so writing
with case statements the obj of the statement is used as the argument for the
'===' operator of each case, so
case obj
when Foo
...
when Bar
end
is the same as
if Foo === obj
...
elsif Bar === obj
...
end
and
case string
when %r/foo/
...
when %r/bar/
end
is the same as
if %r/foo/ === string
...
elsif %/bar/ === string
...
end
so what you've written:
case Symbol
when Symbol
'yes'
else
'no'
end
is the same as
if Symbol === Symbol
'yes'
else
'no'
end
so, '===', when applied to a class, tests if an object is an __instance__ of
that class. so
String === 'a string' #=> true
Array === [42] #=> true
therefore, you are asking if Symbol is an instance of a Symbol - which it is
not. it __is__ equalivalent to Symbol, but that would mean '==' and not
'==='.
case statements can also do this
case obj
when String, Fixnum, Array
when File
when %r/foo/, %r/bar/
else
end
where each element in the list applies '===' to the argument 'obj' to see when
line triggers - it's really powerful.
hth.
-a
···
On Fri, 19 Aug 2005, Daniel Schierbeck wrote:
Could anyone explain to me why this isn't working?
# returns 'no'
case :foo.class
when Symbol then
puts 'yes'
else
puts 'no'
end
.... when this does
# returns 'yes'
case :foo.class.object_id
when Symbol.object_id then
puts 'yes'
else
puts 'no'
end
I'm puzzled...
Thank you in advance,
Daniel Schierbeck
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================