# Easy Question - Get the Class Name

**URL:** <https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788>\
**Category:** Rails\
**Created:** [May 10, 2007, 11:34am UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788 "2007-05-10T11:34:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 10, 2007, 11:34am UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/1 "2007-05-10T11:34:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 10, 2007, 11:36am UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/2 "2007-05-10T11:36:46Z")

</div>

Not sure what you mean? Have a look at to\_s method.

On 5/10/07, flip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://www.web-buddha.co.uk)

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 10, 2007, 11:40am UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/3 "2007-05-10T11:40:46Z")

</div>

asset.class  
asset.class.to\_s

On 5/10/07, flip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 L.

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 10, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/4 "2007-05-10T12:08:51Z")

</div>

On 5/10/07, flip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 11, 2007, 12:16pm UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/5 "2007-05-11T12:16:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 11, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/6 "2007-05-11T17:17:44Z")

</div>

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

result.class.send(:find, 14)

DZ

---

<div class="post-metadata">

**Author:** ![flip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/53a042/32.png) [@flip](https://www.ruby-forum.com/u/flip)\
**Post date:** [May 11, 2007, 6:06am UTC](https://www.ruby-forum.com/t/easy-question-get-the-class-name/103788/7 "2007-05-11T06:06:10Z")

</div>

On 5/10/07, Isak H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
