Easy Question - Get the Class Name

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

Not sure what you mean? Have a look at to_s method.

asset.class asset.class.to_s

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.

That works fine of course, but I think asset.class.name reads better. (Particularly given the title of this thread. :wink:

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.

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.

Sounds like you really just need to use "send" no need to know the class name then:

result.class.send(:find, 14)

DZ