From: timr Date: 2009-11-20T14:05:08+09:00 Subject: eval behaves differently inside a method definition... #lets give strings the ability to evaluate theirselves as if in irb. >> class String >> def irb >> eval self >> end >> end => nil >> "puts 'look at me'" => "puts 'look at me'" >> "puts 'look at me'".irb look at me => nil #good looks like strings can interpret theirselves. But how about this case? >> "class Pumpkin;def pie;puts 'yum';end;end".irb => nil >> Pumpkin.new NameError: uninitialized constant Pumpkin from (irb):287 from /usr/local/bin/irb:12:in `
' #not flying. Is it a problem with the string? Try with eval of the same string: >> eval "class Pumpkin;def pie;puts 'yum';end;end" => nil >> Pumpkin.new => # No problem evaluating the class definition in a string. So eval behaves differently inside a method definition. Could anyone please explain that? Thanks in advance, Tim