From: Trans Date: 2007-06-02T21:34:44+09:00 Subject: Re: nested methods don't really exist?! On Jun 2, 7:55 am, "Robert Dober" wrote: > On 6/2/07, Robert Klemme wrote: > > > > > >> #b is defined only after #a has been invoked at least once. I cannot > > >> think of a scenario where you would want this behavior. > > > > There are dynamic behavior scenarios such as memoize where it could be > > > used. But such cases are pretty rare. So I agree. Unless inner defs > > > are local to their outer def, akin to local variables, they really > > > aren't very useful --being little more than a shortcut for (class << > > > self; self; end).define_method(). > > > That's exactly what they are not. If at all they are a shortcut for > > self.class.define_method(), i.e. methods defined that way a regular > > instance methods. > > > I also think that for memoize and such other mechanisms are far more > > useful than current Ruby nested methods. Actually the current state of > > affairs is a queer mix, because the definition is nested but the scope > > is not (they are neither restricted to the current instance nor to the > > current method). > > Robert I think they are: > # vim: sts=2 sw=2 expandtab nu tw=0: > > class A > def a > def b > 42 > end > end > > end > > p A.new.methods.grep(/^b$/) > A.new.b > [] > nested.rb:13: undefined method `b' for # (NoMethodError) > > Did you overlook David's post? > I get exactly the same behavior than he does on a 1.8.5 Zenwalk > I have the impression that OP got the victim of a "leftover" in his irb session. > > Cheers > Robert > > Maybe that is the major reason for them not being too > > > useful. > > > Kind regards > > > robert Ah, so inner defs won't be allowed after all. Figures, I guess. Why have useful syntax when you can throw an error? :/ To be honest, I'm not sure I understand Ruby's vision for the future these days. Why isn't Ruby further embracing the dynamic revolution it's helped ignite? For example, why aren't we seeing 'def' become a method, just like 'new' already is? I guess maybe the innovation is over and Matz is settling into performance matters only. T.