From: mathew
Date: 2005-08-12T01:46:11+09:00
Subject: Re: Yet Another useless Ruby 2 Idea
Jonas Hartmann wrote:
>mathew wrote:
>> Answer #2:
>>
>> def bar
>> puts "ONE"
>> end
>>
>> def foo
>> puts "TWO"
>>
>> def bar
>> puts "THREE"
>> end
>> end
>>
>> bar
>> foo
>>
>> prints
>>
>> ONE
>> TWO
>>
>> So I don't think it's unambiguous at all.
>
> shouldnt it puts "one" "three" "two"?
No--the second definition of bar is only in scope within foo, so when
bar is called, the only visible definition is the first one, which
prints "ONE". Then foo is called, which prints "TWO". I tested my
examples before posting :-)
The point of nested methods is that it's good programming practice to
limit the scope of things which you don't want to be part of the public
API, and also good practice to limit the size of code inside loops. So
sometimes it's good to define some chunks of processing first, as nested
methods with meaningful names and documentation, then use those chunks
within the main body of the outer method.
mathew
--
WE HAVE TACOS