From: Josh Cheek Date: 2010-08-28T15:39:44+09:00 Subject: Re: Avoiding eval --0016364d2993c6276b048edc7eb6 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Aug 27, 2010 at 10:40 AM, Robert Klemme wrote: > On 27.08.2010 17:24, Andrew Wagner wrote: > > If I have a string like "Math", I know I can use const_get to turn it into >> an instance of Class. Awesome. >> >> But what if I have something like "Math::PI". Am I stuck using eval to >> resolve that? >> > > One typical idiom looks like this: > > a_class = str.split('::').inject(Object) {|cl,n| cl.const_get n} > > > Cheers > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > Here is how Rails implements Robert's suggestion: (see method constantize) http://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb --0016364d2993c6276b048edc7eb6--