Rails 4, Ruby 2
Model has Card and Idiom
and Idiom belongs_to :card
In my Rails console I have an Idiom i and get the card from it:
c=i.card
=> #<Card id: 19, …>
Now I do:
c.is_a?(Card)
=> false
c.instance_of?(Card)
=> false
Ooops! Why false in both cases?
c.class.name
=> “Card”
c.class.name==Card.name
=> true
The class name is the same, but the class isn’t? Please explain…
And now an explanation why I want to do it:
I have a function which is “overloaded”, in that it’s single argument
can be either of a certain clas (in my case, Card (or a subclass of
it)), or a Fixnum, and I need to distinguish between these cases.
I know that it is not the best style of querying the type of a variable
at runtime, but the only alternatives I could think of, would be to
either provide two differently named functions, or have an hash argument
with two types of keywords, and I don’t like both of these solutions.
Aside from this, I’m really curious to know why is_a? doesn’t work
here.