[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

method access to the enclosing method's locals?

Larry Kluger

8/17/2007 3:35:00 PM


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-....

12 Answers

Olivier

8/17/2007 5:57:00 PM

0

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


Jano Svitok

8/17/2007 6:25:00 PM

0

On 8/17/07, Larry Kluger <rubyforum@kluger.com> 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?

Hi,

you may try something using 1. blocks, 2. passing actual binding, 3.
there was something called binding of caller, but I'm not sure whether
it still works, as it was based on a bug in ruby implementation.

Maybe if you post a bit higher perspective on your problem somebody
would come up with a solution...

J.

Robert Klemme

8/17/2007 8:12:00 PM

0

On 17.08.2007 17:35, 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?

What exactly do you need that for? I am curious because I haven't
stumbled across a problem where I needed this for.

Kind regards

robert

David A. Black

8/17/2007 8:23:00 PM

0

Larry Kluger

8/17/2007 10:08:00 PM

0

Hi Everybody,

Thank you for your help. To keep things dry, my main method uses one of
a number of methods, depending on the value of an argument. A hash is
used as a selector to choose the right child method.

The child methods need some data, I wanted to see how they could gain
access to the locals of the calling method, ie, the main method.

I appreciate the posts; I'll continue to pass the arguments, which is
working fine.

Btw, I find it a bit lacking that the syntax checker allows me to
declare a method inside another method since such things don't exist.
(Per Olivier's helpful post.)

Regards,

Larry
--
Posted via http://www.ruby-....

David A. Black

8/17/2007 10:44:00 PM

0

Logan Capaldo

8/17/2007 11:22:00 PM

0

On 8/17/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --
>
> On Sat, 18 Aug 2007, Larry Kluger wrote:
>
> > Hi Everybody,
> >
> > Thank you for your help. To keep things dry, my main method uses one of
> > a number of methods, depending on the value of an argument. A hash is
> > used as a selector to choose the right child method.
> >
> > The child methods need some data, I wanted to see how they could gain
> > access to the locals of the calling method, ie, the main method.
> >
> > I appreciate the posts; I'll continue to pass the arguments, which is
> > working fine.
> >
> > Btw, I find it a bit lacking that the syntax checker allows me to
> > declare a method inside another method since such things don't exist.
> > (Per Olivier's helpful post.)
>
> I wouldn't say it doesn't exist; it's just that the lexically inner
> definition isn't scoped to the outer definition. This is relatively
> new (1.8.6, if I remember correctly), and made it easier to do
> something that was always possible anyway with class_eval +
> define_method.
>
ISTR def foo; def bar; end; end working in 1.8.4 and possibly even
1.8.2. (I didn't hop aboard the ruby train before 1.8.2 though). I
know I've used def foo; def bar before 1.8.6.
>
> David
>
> --
> * Books:
> RAILS ROUTING (new! http://www.awprofessional.com/title/...)
> RUBY FOR RAILS (http://www.manning...)
> * Ruby/Rails training
> & consulting: Ruby Power and Light, LLC (http://www.r...)
>
>

Logan Capaldo

8/17/2007 11:28:00 PM

0

On 8/17/07, Larry Kluger <rubyforum@kluger.com> 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

def initialize
@submeths = {}
end

def dispatch(name, *args)
if @submeths.has_key? name
@submeths[name].call(*args)
else
send(name, *args)
end
end

def submeth(name, &block)
@submeths[name] = block
end

def go2(arg)
# hi = lambda { puts "Hi #{arg}!" }
# personally I would skip the submeths ivar altogether and just do hi.call
# but this is mildly more entertaining
submeth(:hi) { puts "Hi #{arg}" }

a={:h => :hi}
dispatch(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-....
>
>

Peña, Botp

8/18/2007 3:17:00 AM

0

On Behalf Of Larry Kluger:
# Btw, I find it a bit lacking that the syntax checker allows me to
# declare a method inside another method since such things don't exist.

this just a stupid example,

irb(main):040:0> def m1
irb(main):041:1> def m2
irb(main):042:2> "x"
irb(main):043:2> end
irb(main):044:1> end
=> nil
irb(main):045:0> m1
=> nil
irb(main):046:0> m2
=> "x"
irb(main):047:0> m1::m2
=> "x"
irb(main):048:0> def m2
irb(main):049:1> "y"
irb(main):050:1> end
=> nil
irb(main):051:0> m2
=> "y"
irb(main):052:0> m1::m2
=> "x"
irb(main):053:0> m1.m2
=> "x"

kind regards -botp

David A. Black

8/18/2007 11:29:00 AM

0