From: Paul Brannan Date: 2001-08-31T01:44:23+09:00 Subject: [ruby-talk:20585] Weird things with eval (A) If I do this: class Module def foo p self Foo.__send__(:eval, "def bar; puts 'bar'; end") end end class Foo foo end f = Foo.new Module.bar f.bar Then I get: Foo bar test.rb:13: undefined method `bar' for # (NameError) (B) However if I do this: class Foo end Foo.__send__(:eval, "def bar; puts 'bar'; end") f = Foo.new Module.bar f.bar Then from irb I get: NameError: private method `bar' called for Module:Class NameError: private method `bar' called for # (C) However if I do this: class Foo end f = Foo.new Module.bar f.bar Then from irb I get exactly what I would expect: NameError: undefined method `bar' for Module:Class NameError: undefined method `bar' for # (D) Lastly, if I do this: class Foo end Foo.freeze Foo.__send__(:eval, "def bar; puts 'bar'; end") f = Foo.new f.bar Then I get this: NameError: private method `bar' called for # So the questions are: 1) Why in (A) does bar get defined for Module but not for Foo? 2) Why in (B) does bar get defined for both Module and Foo, and why is it private? 3) Why in (D) does Ruby allow me to modify a frozen class, and why is the resulting method private? Paul