From: Sebastian Hungerecker Date: 2007-12-13T06:26:17+09:00 Subject: Re: Turning symbols into class names 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