nested def's

D

Daniel Schüle

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
 
F

Florian Groß

Daniel said:
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 { }.
 
D

David A. Black

--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 --

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
(e-mail address removed)
--8323328-1900748424-1130361714=:13731--
--8323328-1900748424-1130361714=:13731--
 
D

Daniel Schüle

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
 
T

Tanner Burson

------=_Part_13371_28823427.1130362275291
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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
(e-mail address removed)
http://tannerburson.com <---Might even work one day...

------=_Part_13371_28823427.1130362275291--
 
G

gabriele renzi

Daniel Schüle ha scritto:
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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: nested def's"
|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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top