From: Brian Candler Date: 2008-11-27T07:35:00+09:00 Subject: [ruby-core:20132] Re: [Bug #797] bug or feature: local method ? On Thu, Nov 27, 2008 at 03:32:18AM +0900, Francoys wrote: > declaring variables inside a method that are accessibles to all the > nested methods. You can do this with closures: f = lambda { |a| x = 0 g = lambda { |b| x += b } a.each { |e| g[e] } x } puts f[[1,2,3]] Or else you have objects and instance variables. > If someone have a 'rubyly correct' way's to solve my problem. I'm am > open. But keep in mind this: Prolog is a slow langage, so every > performance gain count ! Then I might question whether Ruby is the correct implementation platform for Prolog, except perhaps as a learning exercise (in which case performance isn't important) > As who see, i am facing a real life situation where nested methods seem > very useful and the more elegant solution. Note that when you write def foo ... body end then it's the *execution* of "def foo ... end" which creates and defines the method foo on the object. But the body is not executed until the time when foo is called. Maybe a def which occurs inside a method body (or block) would have to become an alias for foo = lambda { ... } instead. Unfortunately, this particular foo would be called using foo.call or foo[], not foo(). Regards, Brian.