T
Tim Becker
Hi,
I'm encountering weird behavior I don't understand when creating a
case expression that does comparison using classes. Consider the
following:
class SomeClass
end
def case_test elem
case elem.class
when Object
puts "Object"
when String
puts "String"
when SomeClass
puts "SomeClass"
else
puts "Unknown: #{elem.class}"
end
end
executing:
case_test Object.new
case_test "bla"
case_test SomeClass.new
case_test 1
yields:
Object
Object
Object
Object
On the other hand, running:
def if_test elem
if Object == elem.class
puts "Object"
elsif String == elem.class
puts "String"
elsif SomeClass == elem.class
puts "SomeClass"
else
puts "Unknown: #{elem.class}"
end
end
if_test Object.new
if_test "bla"
if_test SomeClass.new
if_test 1
results in:
Object
String
SomeClass
Unknown: Fixnum
I've not been able to find anything that fits searching the bug
database or doing a rudimentary google search, and it would seem to me
that someone else would have come across this before.
Am I missing something?
Thanks,
-tim
Oh and btw: ruby 1.8.5 (2006-08-25) [i686-darwin8.8.1]
I'm encountering weird behavior I don't understand when creating a
case expression that does comparison using classes. Consider the
following:
class SomeClass
end
def case_test elem
case elem.class
when Object
puts "Object"
when String
puts "String"
when SomeClass
puts "SomeClass"
else
puts "Unknown: #{elem.class}"
end
end
executing:
case_test Object.new
case_test "bla"
case_test SomeClass.new
case_test 1
yields:
Object
Object
Object
Object
On the other hand, running:
def if_test elem
if Object == elem.class
puts "Object"
elsif String == elem.class
puts "String"
elsif SomeClass == elem.class
puts "SomeClass"
else
puts "Unknown: #{elem.class}"
end
end
if_test Object.new
if_test "bla"
if_test SomeClass.new
if_test 1
results in:
Object
String
SomeClass
Unknown: Fixnum
I've not been able to find anything that fits searching the bug
database or doing a rudimentary google search, and it would seem to me
that someone else would have come across this before.
Am I missing something?
Thanks,
-tim
Oh and btw: ruby 1.8.5 (2006-08-25) [i686-darwin8.8.1]