From: Mark Hubbart Date: 2004-03-04T05:42:08+09:00 Subject: Re: Why do inner methods count in outer scope? On Mar 2, 2004, at 10:44 PM, Charles Comstock wrote: > def foo(x) > def bar(n) > n + 1 > end > bar(x) > end > > foo(5) # -> 6 > bar(5) # -> 6 > > Why is bar escaping the scope of foo? Is this a bug, or an unintended > feature? Intended feature :) Method definitions are scoped the same way as @variables; the new method is added to the current instance. It doesn't matter if you do it at the "base level" of the class, or if you stick it ten layers down inside blocks of code, it still gets added to the instance. class Foo def bar def quux "quux!" end "defined quux..." end end ==>nil f=Foo.new ==># f.methods - Object.new.methods ==>["bar"] f.quux NoMethodError: undefined method `quux' for # from (irb):11 f.bar ==>"defined quux..." f.quux ==>"quux!" f.methods - Object.new.methods ==>["bar", "quux"] --Mark