From: Kornelius Kalnbach Date: 2009-01-02T16:51:28+09:00 Subject: [ruby-core:21061] Re: [Bug #797] bug or feature: local method ? --Apple-Mail-1-472313858 Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Am 28.11.2008 um 16:31 schrieb Francoys : > 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 ... Except methods...sadly. def, class, module, alias etc. don't return anything. It's hard to explain these things to Python or JavaScript programmers. [murphy] --Apple-Mail-1-472313858 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit


Am 28.11.2008 um 16:31 schrieb Francoys <francois.pr@videotron.ca>:

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 ...
Except methods...sadly. def, class, module, alias etc. don't return anything. It's hard to explain these things to Python or JavaScript programmers.

[murphy]
--Apple-Mail-1-472313858--