Ruby on Rails Saturday, August 30, 2014

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.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/57d76f799458f02d7a4c9494c08bc909%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment