Olivier
8/17/2007 5:57:00 PM
Le vendredi 17 août 2007 17:35, Larry Kluger a écrit :
> 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
Hi,
There is no such thing as a method inside a method. When you wirte 'def hi',
you actually define the method 'hi' at the same level than the method 'go2'.
So, the later cannot acces the data of the former. However, as they are
defined at the same level, they can share data from the instance variables of
the class they are defined in.
In any case, there is no point to do this.
--
Olivier Renaud