From: Dave Aronson Date: 2012-01-27T05:18:53+09:00 Subject: Re: A little assistance please :) On Wed, Jan 25, 2012 at 22:35, Chad Perrin wrote: > No, you couldn't do that either.  I'm explaining why you got the error > Ruby gave you, Just to clarify, I wasn't the OP who got the error, I was just interested in an odd implication of your explanation. >    bar = "Hello" + "World" > >    foo = <    class #{bar} >      def initialize(name='World') >        @name = name >      end > >      def greet >        return "Hello, #{@name}!" >      end >    end >    EOF > > eval(foo) And to point out just HOW horrible that approach is, even aside from the evil of eval... you have to remember to jump through additional hoops and escape the #{@name}. Otherwise, it gets evaluated at here-doc-parse time, as empty. That means hello.greet will just return "Hello, !" (even if you did supply a name). If you escape it, HelloWorld.new.greet properly returns "Hello, World!", and HelloWorld.new('Dave').greet properly returns "Hello, Dave!". Now imagine layering another level of escaping on top of it, like if you were doing things in the class, that in turn required the actual final code to have some escaping. Bleagh. > There may be easier ways to achieve the same end but, in general, don't > do it.  It'll probably just get your code in trouble. Amen. Metaprogramming is very powerful... but with great power etc. -Dave -- Dave Aronson:  Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com