I am using Ruby 1.9.2 and Ruby on Rails 3.2.2. I have the following method:
# Note: The 'class_name' parameter is a constant; that is, it is a model class name.
def my_method(class_name)
case class_name
when Article then make_a_thing
when Comment then make_another_thing
when ... then ...
else raise("Wrong #{class_name}!")
end
end
I would like to understand why, in the case
statement above, it always runs the else
"part" when I execute method calls like my_method(Article)
, my_method(Comment)
and so on.
How can I solve the issue? Does someone have advice how to handle this?
This is because case
calls ===
, and ===
on Class (or specifically Module, which Class descends from) is implemented like so:
mod === obj
→true
orfalse
Case Equality—Returns
true
ifobj
is an instance ofmod
or one ofmod
’s descendants. Of limited use for modules, but can be used in case statements to classify objects by class.
This means that for any constant except Class
& Module
(e.g. Foo
), Foo === Foo
always returns false
. As a result, you always get the else
condition in your case
statement.
Instead just call case
with the object itself, instead of its class, or use if
statements.
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