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