From: Josh Cheek Date: 2010-01-16T05:53:58+09:00 Subject: Nested Function Confusion --000e0cd11768a8bc75047d3a3397 Content-Type: text/plain; charset=ISO-8859-1 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 --000e0cd11768a8bc75047d3a3397--