x == User returns true, but case x statement does not run the block associated with User. What’s happening here?
u = User.new
# => #<User:0x00000100a1e948>
x = u.class
# => User
x == User
# => true
case x
when User
puts "constant"
when "User"
puts "string"
else
puts "nothing?"
end
# => nothing?
Case comparisons use
===rather than==. For many objects the behaviour of===and==is the same, seeNumericandString:But for other kinds of object
===can mean many things, entirely depending on the receiver.For the case of classes,
===tests whether an object is an instance of that class:For Range it checks whether an object falls in that range:
For Procs,
===actually invokes thatProc:For other objects, check their definition of
===to uncover their behaviour. It’s not always obvious, but they usually make some kind of sense..Here is an example putting it all together:
See this link for more info: http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/