nested def's

Discussion in 'Ruby' started by Daniel Schüle, Oct 26, 2005.

  1. Hello *,

    I have some quick questions
    I tried this code

    irb(main):024:0> def a
    irb(main):025:1> def b
    irb(main):026:2> def c
    irb(main):027:3> puts "in c"
    irb(main):028:3> end
    irb(main):029:2> puts "in b"
    irb(main):030:2> c
    irb(main):031:2> end
    irb(main):032:1> puts "in a"
    irb(main):033:1> b
    irb(main):034:1> end
    => nil
    irb(main):035:0> a
    in a
    in b
    in c
    => nil
    irb(main):036:0> class X
    irb(main):037:1> def foo
    irb(main):038:2> puts self
    irb(main):039:2> def bar
    irb(main):040:3> puts self
    irb(main):041:3> puts "bar"
    irb(main):042:3> end
    irb(main):043:2> puts "in foo"
    irb(main):044:2> bar
    irb(main):045:2> end
    irb(main):046:1> end
    => nil
    irb(main):047:0> x = X.new
    => #<X:0x401bd5f0>
    irb(main):048:0> x.foo
    #<X:0x401bd5f0>
    in foo
    #<X:0x401bd5f0>
    bar
    => nil
    irb(main):049:0>

    and it seems to be ok, no warning no error at least
    I remember reading (but dont ask me the article)
    that nested functions are not allowed in ruby
    is this something that has been changed or was this always
    possible?

    thx, Daniel
     
    Daniel Schüle, Oct 26, 2005
    #1
    1. Advertising

  2. Daniel Sch=FCle wrote:

    > I remember reading (but dont ask me the article)
    > that nested functions are not allowed in ruby
    > is this something that has been changed or was this always
    > possible?


    When you are using def in another def you aren't defining a nested=20
    method. You are defining a new regular method on the current object when=20
    the def statement nested into the outer method is run.

    This is usually useless. If you want to reuse bits of code inside a=20
    single method I'd suggest using lambda { }.
     
    Florian Groß, Oct 26, 2005
    #2
    1. Advertising

  3. --8323328-1900748424-1130361714=:13731
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1900748424-1130361714=:13731"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1900748424-1130361714=:13731
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Thu, 27 Oct 2005, Daniel Sch=FCle wrote:

    > I remember reading (but dont ask me the article)
    > that nested functions are not allowed in ruby
    > is this something that has been changed or was this always
    > possible?


    It changed after 1.6.8 (either in 1.7 development or in 1.8.?).


    David

    --=20
    David A. Black

    --8323328-1900748424-1130361714=:13731--
    --8323328-1900748424-1130361714=:13731--
     
    David A. Black, Oct 26, 2005
    #3
  4. well, I played a little and found out that though b is def'ined within a
    one could call b. Same applies for c, and bar within a class as well.
    it behaves as def's are flatten'ed into one namespace
     
    Daniel Schüle, Oct 26, 2005
    #4
  5. ------=_Part_13371_28823427.1130362275291
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    On 10/26/05, Daniel Sch=FCle <-karlsruhe.de> wrote:
    >
    > well, I played a little and found out that though b is def'ined within a
    > one could call b. Same applies for c, and bar within a class as well.
    > it behaves as def's are flatten'ed into one namespace



    And that's exactly what is happening. The only thing you change by nesting
    them that way is the point at which they def statement is processed, nothin=
    g
    more.




    --
    =3D=3D=3DTanner Burson=3D=3D=3D

    http://tannerburson.com <---Might even work one day...

    ------=_Part_13371_28823427.1130362275291--
     
    Tanner Burson, Oct 26, 2005
    #5
  6. Daniel Schüle ha scritto:
    > Hello *,

    <snip>
    >
    > and it seems to be ok, no warning no error at least
    > I remember reading (but dont ask me the article)
    > that nested functions are not allowed in ruby
    > is this something that has been changed or was this always
    > possible?
    >
    > thx, Daniel


    it has been possible for long time (I can't say "always" :)

    But they don't work as you may think, since nested method definition
    still define the method for the same "self", and are not pure functions
    that have local scope.
    See example for better explanation:
    irb(main):001:0> def a
    irb(main):002:1> def b
    irb(main):003:2> def c
    irb(main):004:3> p 'in c'
    irb(main):005:3> end
    irb(main):006:2> p 'in b'
    irb(main):007:2> end
    irb(main):008:1> p 'in a'
    irb(main):009:1> end
    => nil
    irb(main):010:0> b
    NameError: undefined local variable or method `b' for main:Object
    from (irb):10
    irb(main):011:0> a
    "in a"
    => nil
    irb(main):012:0> b
    "in b"
    => nil
     
    gabriele renzi, Oct 26, 2005
    #6
  7. Hi,

    In message "Re: nested def's"
    on Thu, 27 Oct 2005 06:02:10 +0900, Daniel Sch=FCle <-karlsr=
    uhe.de> writes:

    |and it seems to be ok, no warning no error at least
    |I remember reading (but dont ask me the article)
    |that nested functions are not allowed in ruby
    |is this something that has been changed or was this always
    |possible?

    It's changed, but don't use. Its behavior may change again in the
    future.

    matz.
     
    Yukihiro Matsumoto, Oct 27, 2005
    #7
    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. Jiong Feng
    Replies:
    0
    Views:
    877
    Jiong Feng
    Nov 19, 2003
  2. Sean Ross
    Replies:
    3
    Views:
    135
    Aredridel
    Dec 25, 2003
  3. Replies:
    7
    Views:
    170
  4. planetthoughtful

    Newbie: def must come before call to def?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    4
    Views:
    136
    Pit Capitain
    Mar 12, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    213
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page