From: Francoys Date: 2008-11-29T00:31:38+09:00 Subject: [ruby-core:20152] Re: [Bug #797] bug or feature: local method ? This is a multi-part message in MIME format. --Boundary_(ID_0Z5tPB9IEfRmxsNUMLt8FQ) Content-type: text/plain; charset=UTF-8 Content-transfer-encoding: 7BIT Diogo Lisboa wrote: > On Wed, Nov 26, 2008 at 4:32 PM, Francoys wrote: > >> declaring variables inside a method that are accessibles to all the >> nested methods. >> > > This isn't possible though. > > def foo > bar = 10 > def gazonk > p bar > end > end > > >>> gazonk # => undefined local variable or method `gazonk' >>> foo # => nil >>> gazonk # => undefined local variable or method `bar' >>> > > > This doesn't work because the chained method is in a whole new scope > which doesn't know anything about the outer method's local variables. > > But, however odd this functionality is, I think it would be useful for > the enclosing method to inherit the outer scope. You'd get something > like named closures, and calling `gazonk' after `foo' would be able to > print 10. > > However, this wouldn't be consistent, meaning, it should have to work > if you declared a local variable in a class definition and all the > methods would be aware of that; at this point, things get ugly. > > I think the idea of 'named closure' could be interesting (I don't know > what consequences this would have, but sounds like a feature more than > a bug). It beats returning lambdas in an array. > > Would you, enlightened ones, share your opinions on this? On the idea > of the local methods being a bug or feature, I don't think they count > much as a feature the way they currently are. > > > Diogo > > > Try this one: def foo @bar = 10 def gazonk print @bar end gazonk end foo Everything is an object in Ruby ... --Boundary_(ID_0Z5tPB9IEfRmxsNUMLt8FQ) Content-type: text/html; charset=UTF-8 Content-transfer-encoding: 8BIT Diogo Lisboa wrote:
On Wed, Nov 26, 2008 at 4:32 PM, Francoys <francois.pr@videotron.ca> wrote:
  
declaring variables inside a method that are accessibles to all the
nested methods.
    

This isn't possible though.

def foo
  bar = 10
  def gazonk
    p bar
  end
end

  
gazonk # => undefined local variable or method `gazonk'
foo  # => nil
gazonk # => undefined local variable or method `bar'
      


This doesn't work because the chained method is in a whole new scope
which doesn't know anything about the outer method's local variables.

But, however odd this functionality is, I think it would be useful for
the enclosing method to inherit the outer scope. You'd get something
like named closures, and calling `gazonk' after `foo' would be able to
print 10.

However, this wouldn't be consistent, meaning, it should have to work
if you declared a local variable in a class definition and all the
methods would be aware of that; at this point, things get ugly.

I think the idea of 'named closure' could be interesting (I don't know
what consequences this would have, but sounds like a feature more than
a bug). It beats returning lambdas in an array.

Would you, enlightened ones, share your opinions on this? On the idea
of the local methods being a bug or feature, I don't think they count
much as a feature the way they currently are.


Diogo


  
Try this one:

def foo
�� @bar = 10
�� def gazonk
������ print @bar
�� end
�� gazonk
end

foo


Everything is an object in Ruby ...
--Boundary_(ID_0Z5tPB9IEfRmxsNUMLt8FQ)--