Ruby on Rails Sunday, August 31, 2014

As colin mentioned, this is an active record relation that looks like a card. I believe it's actually an association proxy (I dont have the rails code open here)

Also, in rails, best practice is to operate on duck types, not strict types. You should never have to is_a? anything. This hints at design flaws within your application.

Remember, .class is simply a method defined on an object and can be overriden too.

On Saturday, August 30, 2014 6:21:48 AM UTC-7, Ruby-Forum.com User wrote:
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/963eaa9e-323f-4aa8-a70d-ed74c3e75291%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment