Turning symbols into class names

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!

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

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.

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.

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

···

--
NP: In Flames - Dead Eternity
Jabber: sepp2k@jabber.org
ICQ: 205544826

const_get?

Kernel.const_get(:Foo).methods

Hope that helps

Regards,
Lee

···

--
Posted via http://www.ruby-forum.com/.

Gordon Thiesfeld wrote:

Regards,

Gordon

Damnit, you were too quick :stuck_out_tongue:

···

--
Posted via http://www.ruby-forum.com/\.

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. :slight_smile:

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

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. :slight_smile: 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. :slight_smile:

> 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

···

On Dec 12, 4:26 pm, Sebastian Hungerecker <sep...@googlemail.com> wrote:

HTH,
Sebastian
--
NP: In Flames - Dead Eternity
Jabber: sep...@jabber.org
ICQ: 205544826

Rails has something even easier:

:foo.to_s.constantize -> Foo

···

On Dec 12, 2007 4:39 PM, Samantha <rubygeekgirl@gmail.com> wrote:

On Dec 12, 4:26 pm, Sebastian Hungerecker <sep...@googlemail.com> > 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. :slight_smile:

> > 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.
>
> 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. :slight_smile: 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. :slight_smile:

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

> HTH,
> Sebastian
> --
> NP: In Flames - Dead Eternity
> Jabber: sep...@jabber.org
> ICQ: 205544826

Oops, that needs to be:

:foo.to_s.capitalize.constantize -> Foo

···

On Dec 12, 2007 4:58 PM, Jamey Cribbs <jcribbs@netpromi.com> wrote:

Rails has something even easier:

:foo.to_s.constantize -> Foo

On Dec 12, 2007 4:39 PM, Samantha <rubygeekgirl@gmail.com> wrote:

>
>
> On Dec 12, 4:26 pm, Sebastian Hungerecker < sep...@googlemail.com> > > 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. :slight_smile:
>
> > > 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.
> >
> > 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. :slight_smile: 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. :slight_smile:
>
> > > 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
>
> > HTH,
> > Sebastian
> > --
> > NP: In Flames - Dead Eternity
> > Jabber: sep...@jabber.org
> > ICQ: 205544826
>
>
>