I can not for the life of me figure this out and I know it is easy but I can''t seem to find the answer in the API docs or looking at different plugins but I have a variable that can be different classes (objects, whatever) and I need to get the class name. here is an example asset = User.find(1) how do I get "User" as a string returned from the asset thanks I know this is simple but can''t figure it out --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Not sure what you mean? Have a look at to_s method. On 5/10/07, flip <flipper.f-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I can not for the life of me figure this out and I know it is easy but > I can''t seem to find the answer in the API docs or looking at > different plugins but I have a variable that can be different classes > (objects, whatever) and I need to get the class name. > > here is an example > > asset = User.find(1) > > how do I get "User" as a string returned from the asset > > thanks I know this is simple but can''t figure it out > > > > >-- http://www.web-buddha.co.uk --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
asset.class asset.class.to_s On 5/10/07, flip <flipper.f-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I can not for the life of me figure this out and I know it is easy but > I can''t seem to find the answer in the API docs or looking at > different plugins but I have a variable that can be different classes > (objects, whatever) and I need to get the class name. > > here is an example > > asset = User.find(1) > > how do I get "User" as a string returned from the asset > > thanks I know this is simple but can''t figure it out > > > > >-- Best regards, Yuri Leikind --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 5/10/07, flip <flipper.f-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I can not for the life of me figure this out and I know it is easy but > I can''t seem to find the answer in the API docs or looking at > different plugins but I have a variable that can be different classes > (objects, whatever) and I need to get the class name. >Sure you really want/need the class name? If your objects are similar enough that you store them in the same collection, does it really matter what class they are? If they don''t respond to the same method(s), why mix them in the first place?> here is an example > > asset = User.find(1) > > how do I get "User" as a string returned from the asset >asset.class.to_s .class returns the Class of your asset, while .to_s returns the name of that class or module.> thanks I know this is simple but can''t figure it out > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 5/10/07, Isak Hansen <isak.hansen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> asset.class.to_s > > .class returns the Class of your asset, while .to_s returns the name > of that class or module.That works fine of course, but I think asset.class.name reads better. (Particularly given the title of this thread. ;-) OTOH, if you''re doing string interpolation, the fact that #to_s returns the name means asset.class is sufficient, which is useful to know too. Regards, George. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks everyone it was the one thing I didn''t try and man do I feel a little dumb. To answer some of the questions the asset can be any one of 5 different classes that I end up running a polymorphic find on so I needed to get the name of the class to pass to the find. On May 11, 12:05 am, George <george.og...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 5/10/07, Isak Hansen <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > asset.class.to_s > > > .class returns the Class of your asset, while .to_s returns the name > > of that class or module. > > That works fine of course, but I think asset.class.name reads better. > (Particularly given the title of this thread. ;-) > > OTOH, if you''re doing string interpolation, the fact that #to_s > returns the name means asset.class is sufficient, which is useful to > know too. > > Regards, > George.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sounds like you really just need to use "send" no need to know the class name then: result.class.send(:find, 14) DZ On May 11, 5:15 am, flip <flippe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks everyone it was the one thing I didn''t try and man do I feel a > little dumb. To answer some of the questions the asset can be any one > of 5 different classes that I end up running a polymorphic find on so > I needed to get the name of the class to pass to the find. > > On May 11, 12:05 am, George <george.og...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 5/10/07, Isak Hansen <isak.han...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > asset.class.to_s > > > > .class returns the Class of your asset, while .to_s returns the name > > > of that class or module. > > > That works fine of course, but I think asset.class.name reads better. > > (Particularly given the title of this thread. ;-) > > > OTOH, if you''re doing string interpolation, the fact that #to_s > > returns the name means asset.class is sufficient, which is useful to > > know too. > > > Regards, > > George.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---