# Turning symbols into class names

**URL:** <https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942>\
**Category:** Ruby\
**Created:** [December 12, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942 "2007-12-12T22:18:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/1 "2007-12-12T22:18:04Z")

</div>

Long time no see!

Hopefully I can get some insight into something I’m dealing with, as I  
have in the past!

I’m working on something in Rails, however, I believe this to be more  
of a Ruby question than anything having to do with Rails.

I’m grabbing all the associations from a Class, and then putting those  
associations into an Array. What I get back is an Array of symbols.

Now, I need to do the same thing for those symbols… however, I need  
to somehow get Ruby to recognize those symbols as Classes.

So, let’s say in my array, I have a symbol by the name of :foos, that /  
should/ correspond with the Foo class.

So…

array looks like: [:foos, :bars]

I need to be able to do something along the lines of:

array[0].to\_s.capitalize.singularize.my\_method\_to\_grab\_associations

Well, obviously that will give me Foo, but it thinks Foo is a string.

Which leads me to my question -  
How do I get my method to give me back what I’m looking for on Foo,  
rather than complaining that Foo is a string?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/2 "2007-12-12T22:25:13Z")

</div>

> Which leads me to my question -  
> How do I get my method to give me back what I’m looking for on Foo,  
> rather than complaining that Foo is a string?
> 
> Thanks in advance!

I believe you’re looking for Module#const\_get.

Regards,

Gordon

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/3 "2007-12-12T22:27:25Z")

</div>

const\_get?

Kernel.const\_get(:Foo).methods

Hope that helps

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/4 "2007-12-12T22:28:05Z")

</div>

Gordon T. wrote:

> Regards,
> 
> Gordon

Damnit, you were too quick 😛

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/5 "2007-12-12T22:40:27Z")

</div>

On Dec 12, 4:26 pm, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Samantha wrote:
> 
> > Now, I need to do the same thing for those symbols… however, I need  
> > to somehow get Ruby to recognize those symbols as Classes.
> 
> Nitpick: You need ruby to give you the class with a certain name. You don’t  
> need ruby to “recognize” the symbols as something they’re not.

Semantics, semantics, semantics. 🙂

> > Well, obviously that will give me Foo, but it thinks Foo is a string.
> 
> You make it sound as if ruby was wrong in thinking so. “Foo” _is_ a string.

Oh, I know that “Foo” is a string. I just needed a way to turn that  
string into something else. 🙂 Of course if anyone is wrong, it’s me,  
and not Ruby - I’ve just been working on something for a long amount  
of time which has my ‘grr’ factor raised a lil bit. 🙂

> > Which leads me to my question -  
> > How do I get my method to give me back what I’m looking for on Foo,  
> > rather than complaining that Foo is a string?
> 
> By passing the string to const\_get, which will return the value of the  
> constant with the name “Foo” and then calling your methods on that value  
> (which will be the class object if Foo is a class).  
> [:foos, :bars].map {|name| Object.const\_get(name.to\_s.capitalize.singularize)}  
> should return [Foo, Bar], Foo and Bar being class objects.

Thank you, Sebastian! That did _exactly_ what I needed, which is why  
after banging my head incessantly on this for a while now, I decided  
to come here - where those who know much, much more than I, reside.

-Samantha

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/6 "2007-12-12T22:27:33Z")

</div>

Samantha wrote:

> Now, I need to do the same thing for those symbols… however, I need  
> to somehow get Ruby to recognize those symbols as Classes.

Nitpick: You need ruby to give you the class with a certain name. You  
don’t  
need ruby to “recognize” the symbols as something they’re not.

> Well, obviously that will give me Foo, but it thinks Foo is a string.

You make it sound as if ruby was wrong in thinking so. “Foo” _is_ a  
string.

> Which leads me to my question -  
> How do I get my method to give me back what I’m looking for on Foo,  
> rather than complaining that Foo is a string?

By passing the string to const\_get, which will return the value of the  
constant with the name “Foo” and then calling your methods on that value  
(which will be the class object if Foo is a class).  
[:foos, :bars].map {|name|  
Object.const\_get(name.to\_s.capitalize.singularize)}  
should return [Foo, Bar], Foo and Bar being class objects.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/7 "2007-12-12T23:03:20Z")

</div>

Oops, that needs to be:

:foo.to\_s.capitalize.constantize -\> Foo

---

<div class="post-metadata">

**Author:** ![Samantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a8b319/32.png) [@Samantha](https://www.ruby-forum.com/u/Samantha)\
**Post date:** [December 12, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/turning-symbols-into-class-names/122942/8 "2007-12-12T22:58:38Z")

</div>

Rails has something even easier:

:foo.to\_s.constantize -\> Foo
