Nested def's

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 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 method. You are defining a new regular method on the current object when the def statement nested into the outer method is run.

This is usually useless. If you want to reuse bits of code inside a single method I'd suggest using lambda { }.

Hi --

···

On Thu, 27 Oct 2005, Daniel Schüle 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

--
David A. Black
dblack@wobblini.net

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 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" :slight_smile:

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

Hi,

···

In message "Re: nested def's" on Thu, 27 Oct 2005 06:02:10 +0900, Daniel Schüle <uval@rz.uni-karlsruhe.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.

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, nothing
more.

···

On 10/26/05, Daniel Schüle <uval@rz.uni-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

--
===Tanner Burson===
tanner.burson@gmail.com
http://tannerburson.com <---Might even work one day...