From: timr Date: 2009-11-20T16:00:05+09:00 Subject: Re: eval behaves differently inside a method definition... > The parent class effectively acts as a namespace for the subclass. BTW, I really don't think I should call it a "subclass", as that seems to imply inheritence - none of that here. Maybe someone else can tell us the proper name for this structure/pattern. > I understand--how about nested class or encapsulated class. > So you probably understand the issue by now, eval within the String class (whether or not inside a method definition) creates a "subclass" of sorts, and hence you can't call it directly outside. If you tried to call "String::Pumpkin.new" instead after using your "irb" method, it would have worked (try it out). Alternately, you could have done it this way: > > class String > def irb > eval self > end > end > "class ::Pumpkin;def pie;puts 'yum';end;end".irb > Pumpkin.new.pie > > Hope that helps. > - Ehsan > Ehsan, nice explanation! Hmmm. That means that you could make a binding to main >> SPECIAL_BINDING = binding and add that to the eval argument to get it back to the main scope where eval seems to work on anything: class String def irb eval(self, SPECIAL_BINDING) end end Now it should all work! >> "puts 'yohoo'" => "puts 'yohoo'" >> "puts 'yohoo'".irb yohoo => nil >> "class What;def cool;puts 'is totally awesome';end;end".irb => nil >> What.new => # >> What.new.cool is totally awesome => nil >> Mucho Gracias, amigo!!! Tim