Robert Klemme
5/1/2008 11:56:00 AM
On 01.05.2008 11:13, Thomas Hafner wrote:
> Paul McMahon <pm@ubit.com> wrote/schrieb <48197C1A.2050807@ubit.com>:
>
>> Why not just use methods?
>
> Inside the block, that is passed to function "let", many variables
> could be referenced which are defined outside that block. Using
> methods, these variables had to be passed explicitely. Your question
> is a special case of the more general question: Why using blocks at
> all when programming Ruby? Every block could be replaced by a method.
I had read Paul's reply a little differently: this was not about blocks
vs. methods but about refactoring code. If you find yourself needing to
separate a lot of scopes inside a method, chances are that your code
would benefit from refactoring into several methods.
And yes, in that case you do need to pass data explicitly that you need
inside but this has the advantage of making it clearer which state is
needed in each section.
Btw, Thomas, this definition serves the same purpose as yours and is
simpler:
def let;yield;end
You could as well do
1.times {
x=20
# ...
}
:-)
Kind regards
robert