Nested Function Confusion

J

Josh Cheek

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

Hi, couldn't find any explanation of nested functions (other than an
acknowledgment of their existence) in Programming Ruby or the Ruby
Programming Language, so thought I'd see what people here could tell me.

I'm having a difficult time understanding what is going on in this example:
def test
def test2
end
end

RUBY_VERSION # => "1.8.6"
defined? test # => "method"
defined? test2 # => nil

defined? test::test2 # => "method"
defined? test2 # => "method"

defined? test # => "method"
methods.grep( /test/ ) # => ["test2"]

The first time I check whether test2 is defined, it returns nil, the second
time it returns "method"
When I check if test is defined, it returns 'method' but when I type
methods.grep(/test/) it only finds "test2"


Also, when should nested functions/methods be used?
Are nested functions redefined upon each call to the outer function?
Are there scope restrictions that would make it sensible to use nested
functions to enforce encapsulation of interdependent logic?

Thanks,
Josh
 
J

Justin Collins

Josh said:
Hi, couldn't find any explanation of nested functions (other than an
acknowledgment of their existence) in Programming Ruby or the Ruby
Programming Language, so thought I'd see what people here could tell me.

I'm having a difficult time understanding what is going on in this example:
def test
def test2
end
end

RUBY_VERSION # => "1.8.6"
defined? test # => "method"
defined? test2 # => nil

defined? test::test2 # => "method"
defined? test2 # => "method"

defined? test # => "method"
methods.grep( /test/ ) # => ["test2"]

The first time I check whether test2 is defined, it returns nil, the second
time it returns "method"
When I check if test is defined, it returns 'method' but when I type
methods.grep(/test/) it only finds "test2"


Also, when should nested functions/methods be used?
Are nested functions redefined upon each call to the outer function?
Are there scope restrictions that would make it sensible to use nested
functions to enforce encapsulation of interdependent logic?

Thanks,
Josh


Ruby doesn't support defining nested functions like this very well, so I
would say they should never be used, if possible. Instead, use lambdas,
procs, or other closure-like constructs. Try searching for "ruby nested
methods" and you will find past discussions of this.

Nested functions are not really nested, but are flattened into the same
scope. Try running test2, then test1, then test2 again.

Yes, nested functions will be redefined each time the def...end code is
run. You can verify by having two different definitions in an if statement.

Also, defined? is a strange and mystical beast that should probably be
avoided.

-Justin
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top