From: david karapetyan Date: 2007-08-18T16:58:26+09:00 Subject: Re: method access to the enclosing method's locals? ------=_Part_111374_2317569.1187423911006 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Sorry, the code below doesn't work. There are problems with variable scoping. On 8/18/07, david karapetyan wrote: > > Here's one way. First define the closure then use define_method to turn > the > closure into an actual method. > > def go2(arg) > closure = proc { puts "Hi #{arg}" } > define_method(:hi,closure) > end > > I haven't tested the above code so I'm not sure if it really works. > > On 8/18/07, david karapetyan wrote: > > > > The general concept involved is that of a closure and it is certainly > > possible to define closures in ruby and at some point or another someone > > pointed out a way of turning closures into methods I don't remember how > > though. > > > > On 8/17/07, Larry Kluger wrote: > > > > > > > > > Hi, > > > > > > How can a method gain access to the enclosing method's locals? > > > > > > def go2(arg) > > > def hi > > > puts "Hi #{arg}!" > > > end > > > > > > a={:h => :hi} > > > send(a[:h]) > > > end > > > > > > go2 'Larry' ==>> NameError: undefined local variable or method `arg' > for > > > > > > main:Object > > > > > > In go2, is there a clean way for the hi method to have access to the > > > arg local? > > > > > > Thanks! > > > > > > Larry > > > -- > > > Posted via http://www.ruby-forum.com/ . > > > > > > > > > ------=_Part_111374_2317569.1187423911006--