nested methods

Discussion in 'Ruby' started by 7stud --, Mar 20, 2009.

  1. 7stud --

    7stud -- Guest

    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/.
     
    7stud --, Mar 20, 2009
    #1
    1. Advertising

  2. 7stud --

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/3/20 7stud -- <>

    > 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
     
    James Coglan, Mar 20, 2009
    #2
    1. Advertising

  3. 7stud --

    Leo Guest

    > 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.).
     
    Leo, Mar 20, 2009
    #3
  4. 7stud --

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/3/20 Leo <>

    > > 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
     
    James Coglan, Mar 20, 2009
    #4
  5. 7stud --

    Brian Adkins Guest

    James Coglan <> writes:

    > [Note: parts of this message were removed to make it a legal post.]
    >
    > 2009/3/20 Leo <>
    >
    >> > 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/
     
    Brian Adkins, Mar 20, 2009
    #5
  6. 7stud --

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/3/20 Brian Adkins <>

    > James Coglan <> writes:
    >
    > > [Note: parts of this message were removed to make it a legal post.]
    > >
    > > 2009/3/20 Leo <>
    > >
    > >> > 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.
     
    James Coglan, Mar 20, 2009
    #6
  7. 7stud --

    Leo Guest

    > 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.
     
    Leo, Mar 20, 2009
    #7
  8. 7stud --

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    2009/3/20 Leo <>

    > > 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.
     
    James Coglan, Mar 20, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,239
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    674
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    451
  4. Ultrus
    Replies:
    3
    Views:
    399
    Stefan Behnel
    Jul 9, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    353
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page