From: Greg Weeks Date: 2007-11-12T12:15:25+09:00 Subject: Re: bug in nested defs (?) After some more thought, the rule that instance methods go into the lexically enclosing class has surprised me again. Let x = []. I had thought that the following two methods behaved identically (except for "foo vs bar", of course): def x.define_foo def foo ; "foo" ; end end class << x def define_bar def bar ; "bar" ;end end end However, in the first case the lexically enclosing class is Object (well, you know what I mean), and in the second case the lexically enclosing class is the singleton class of x. So the rule says (correctly) that the two are different: x.define_foo Object.instance_methods.include? "foo" # true x.singleton_methods.include? "foo" # false x.define_bar Object.instance_methods.include? "bar" # false x.singleton_methods.include? "bar" # true Good!, I think. But there is still one case where I don't know where the "def" should go: x.instance_eval { def baz ; "baz" ; end } What class acts as the effective enclosing class of the def? Is it Object, the actual enclosing class? Or Array, the class of x? Or is it the singleton class of x? The answer is the latter: x.singleton_methods.include? "baz" # true Whew! -- Posted via http://www.ruby-forum.com/.