From: Joel VanderWerf Date: 2004-03-04T08:02:12+09:00 Subject: Re: Why do inner methods count in outer scope? Mark Hubbart wrote: > > 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 ^^^^^^^^ Not exactly. It can be added to the current class, if that is the scope: class A def foo def bar; p "BAR"; end end end a = A.new b = A.new a.foo b.bar # ==> "BAR"