I'm using Ruby's case
syntax to set up some simple logic based on self.class
as follows:
case self.class
when FirstClass
do stuff....
when SecondClass
do other stuff...
end
I soon realized this always returns nil.
Upon closer investigation, I found that case
uses ===
rather than ==
to check equality. When running self.class == FirstClass
in my terminal I get true
as expected, however self.class === FirstClass
returns false
.
Looking into the ruby docs, I found the following explanation of ===
:
Case Equality – For class Object, effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements.
Can anyone out there shed some light on what may be happening? Thanks in advance.
The clue is in “typically overridden by descendants to provide meaningful semantics in case statements”, in particular Module
overrides it:
Case Equality — Returns
true
if obj is an instance of mod or one of mod’s descendants. Of limited use for modules, but can be used incase
statements to classify objects by class.
So for modules ===
acts very much like the is_a?
method (in fact it just calls the same implementing function in MRI Ruby, rb_obj_is_kind_of
). In your example it evaluates to false
because self.class
isn’t an instance of FirstClass
. It’s likely to just be an instance of Class
. self
alone however could be an instance:
case self
when FirstClass
do stuff....
when SecondClass
do other stuff...
end
(Although I think your design might not be quite right, testing the class of an object is usually a code smell. Instead you probably should have different implementations of a method in the objects.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With