Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > nested def's

Reply
Thread Tools

nested def's

 
 
Daniel Schüle
Guest
Posts: n/a
 
      10-26-2005
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

 
Reply With Quote
 
 
 
 
Florian Groß
Guest
Posts: n/a
 
      10-26-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?


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 { }.



 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      10-26-2005
--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
http://www.velocityreviews.com/forums/(E-Mail Removed)
--8323328-1900748424-1130361714=:13731--
--8323328-1900748424-1130361714=:13731--


 
Reply With Quote
 
Daniel Schüle
Guest
Posts: n/a
 
      10-26-2005
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

 
Reply With Quote
 
Tanner Burson
Guest
Posts: n/a
 
      10-26-2005
------=_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 <(E-Mail Removed)-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
(E-Mail Removed)
http://tannerburson.com <---Might even work one day...

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


 
Reply With Quote
 
gabriele renzi
Guest
Posts: n/a
 
      10-26-2005
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


 
Reply With Quote
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      10-27-2005
Hi,

In message "Re: nested def's"
on Thu, 27 Oct 2005 06:02:10 +0900, Daniel Sch=FCle <(E-Mail Removed)-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.


 
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
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 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