From: Charles Comstock Date: 2004-03-03T19:24:46+09:00 Subject: Re: Why do inner methods count in outer scope? Tim Bates wrote: > nobu.nokada@softhome.net wrote: > >> At Wed, 3 Mar 2004 15:44:46 +0900, >> Charles Comstock wrote in [ruby-talk:94119]: >> >>> Why is bar escaping the scope of foo? Is this a bug, or an >>> unintended feature? > > > Probably quite intentional; I quite often use it for dynamic > (re)definition of methods and would be annoyed if it went away... > > I suggest if you need this behaviour, use a proc literal assigned to a > local variable, thus: > > def foo(x) > bar = proc do |n| > n + 1 > end > bar.call(x) # or bar[x] > end > > foo(5) # -> 6 > bar(5) # -> wtf? I don't know what bar is. > > > Tim. > Nah, I don't really need it, I was just trying to scope out what the language was capable of scoping wise. Out of curiosity, why don't you just use an eval to change function definitions? It would be much nicer I think if it worked as another way to generate a closure, but bound in the outer scope. But maybe I'm just not following what your using it for. Charles Comstock