From: Joey Date: 2006-05-31T17:15:17+09:00 Subject: Re: class from string ------=_Part_27632_21087394.1149063314128 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline http://www.eachmapinject.com/class_name.html j`ey http://www.eachmapinject.com $_=%q{$_=%q{Q};sub(/Q/,$_);print};sub(/Q/,$_);print On 5/31/06, Ezra Zygmuntowicz wrote: > > > On May 30, 2006, at 4:52 PM, Dan Kirkwood wrote: > > > Is there a better way to get to a class when you have the class > > name in > > a string? > > (This is rails-related, btw).. For instance, if I have a string > > clname > > = 'Train', and I want to access a class method Train#label. I > > can do > > this: > > > > clname = 'Train' > > label = eval "#{clname}.label" > > > > Is there a better way? > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > In rails you can use #constantize > > clname = 'Train' > clname.constantize.label > > But you really should try to search before posting. This was asked a > bunch of times in the last few weeks. > > Cheers- > -Ezra > > ------=_Part_27632_21087394.1149063314128--