Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > nested methods

Reply
Thread Tools

nested methods

 
 
7stud --
Guest
Posts: n/a
 
      03-20-2009
Hi,

This example is from pickaxe2(I can't find the page):

def toggle

def toggle
puts "subsequent"
end

puts "first"
end

toggle
toggle
toggle

--output:--
first
subsequent
subsequent


Can someone explain why the nested method hides the outer method?

Thanks.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Coglan
Guest
Posts: n/a
 
      03-20-2009
[Note: parts of this message were removed to make it a legal post.]

2009/3/20 7stud -- <(E-Mail Removed)>

> Hi,
>
> This example is from pickaxe2(I can't find the page):
>
> def toggle
>
> def toggle
> puts "subsequent"
> end
>
> puts "first"
> end
>
> toggle
> toggle
> toggle
>
> --output:--
> first
> subsequent
> subsequent
>
>
> Can someone explain why the nested method hides the outer method?



The first call redefines toggle on the current object (which here seems to
be the global main object) and then returns "first". After this, toggle has
been replaced by the inner method so it returns "subsequent" for all further
calls.

--
James Coglan
http://github.com/jcoglan

 
Reply With Quote
 
 
 
 
Leo
Guest
Posts: n/a
 
      03-20-2009
> Can someone explain why the nested method hides the outer method?

There are no nested methods in ruby (similar to smalltalk, eiffel,
java and unlike scheme, python etc.).

 
Reply With Quote
 
James Coglan
Guest
Posts: n/a
 
      03-20-2009
[Note: parts of this message were removed to make it a legal post.]

2009/3/20 Leo <(E-Mail Removed)>

> > Can someone explain why the nested method hides the outer method?

>
> There are no nested methods in ruby (similar to smalltalk, eiffel,
> java and unlike scheme, python etc.).



This statement could confuse people given the above. Clearly you can define
methods inside other methods, the point is that the 'inner' method is not
local to the outer one. The inner one becomes defined in the same scope as
the outer one, and does not remember the environment it was created in.
Methods are not closures, unlike blocks/procs/lambdas.

--
James Coglan
http://github.com/jcoglan

 
Reply With Quote
 
Brian Adkins
Guest
Posts: n/a
 
      03-20-2009
James Coglan <(E-Mail Removed)> writes:

> [Note: parts of this message were removed to make it a legal post.]
>
> 2009/3/20 Leo <(E-Mail Removed)>
>
>> > Can someone explain why the nested method hides the outer method?

>>
>> There are no nested methods in ruby (similar to smalltalk, eiffel,
>> java and unlike scheme, python etc.).

>
>
> This statement could confuse people given the above. Clearly you can define
> methods inside other methods, the point is that the 'inner' method is not
> local to the outer one. The inner one becomes defined in the same scope as
> the outer one,


Isn't being defined in the same scope the antithesis of nesting?

> and does not remember the environment it was created in.
> Methods are not closures, unlike blocks/procs/lambdas.
>
> --
> James Coglan
> http://github.com/jcoglan
>


--
Brian Adkins
http://lojic.com/
 
Reply With Quote
 
James Coglan
Guest
Posts: n/a
 
      03-20-2009
[Note: parts of this message were removed to make it a legal post.]

2009/3/20 Brian Adkins <(E-Mail Removed)>

> James Coglan <(E-Mail Removed)> writes:
>
> > [Note: parts of this message were removed to make it a legal post.]
> >
> > 2009/3/20 Leo <(E-Mail Removed)>
> >
> >> > Can someone explain why the nested method hides the outer method?
> >>
> >> There are no nested methods in ruby (similar to smalltalk, eiffel,
> >> java and unlike scheme, python etc.).

> >
> >
> > This statement could confuse people given the above. Clearly you can

> define
> > methods inside other methods, the point is that the 'inner' method is not
> > local to the outer one. The inner one becomes defined in the same scope

> as
> > the outer one,

>
> Isn't being defined in the same scope the antithesis of nesting?
>
> > and does not remember the environment it was created in.
> > Methods are not closures, unlike blocks/procs/lambdas.

>


Depends what you mean by nesting, which was supposed to be my point -- that
the methods are lexically nested but not dynamically nested. In other words
their lexical nesting does not imply lexical scope or closures in this case.

 
Reply With Quote
 
Leo
Guest
Posts: n/a
 
      03-20-2009
> Depends what you mean by nesting, which was supposed to be my point -- that
> the methods are lexically nested but not dynamically nested.


I don't think I understand what you mean with "lexically nested" here.
The point is that the inner method replaces the outer one. Nothing
else. Maybe we could call that stacked methods or whatever. But since
the inner method cannot refer to local variables of the outer method
the word nested IMHO simply makes no sense.

The reason why I posted my possibly confusing statement was because
when I started using ruby, I was myself confused by the lack of nested
methods in ruby. It became easier for me to code in ruby after I
simply accepted that ruby doesn't have nested methods/functions like
many functional languages.

 
Reply With Quote
 
James Coglan
Guest
Posts: n/a
 
      03-20-2009
[Note: parts of this message were removed to make it a legal post.]

2009/3/20 Leo <(E-Mail Removed)>

> > Depends what you mean by nesting, which was supposed to be my point --

> that
> > the methods are lexically nested but not dynamically nested.

>
> I don't think I understand what you mean with "lexically nested" here.
> The point is that the inner method replaces the outer one. Nothing
> else. Maybe we could call that stacked methods or whatever. But since
> the inner method cannot refer to local variables of the outer method
> the word nested IMHO simply makes no sense.




I meant 'lexically' as in the methods are syntactically nested. You're
right, since methods are not closures it makes no sense to talk of them as
being nested/stacked/whatever when talking about how the code is executed,
they are nested in appearance only.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments