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.
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:
But you can’t pass arguments to the scope. Whatever that might be good
for.
Yes, that was my intention, especially with regard to Ruby 1.9 and its
local block parameters - those preceded with “;”. I think the changes
on those parameters are limited to inside the block, aren’t they? Then
my Ruby-method “let” has much in common with Scheme’s syntactical form
“let”, therefore the name. But nevertheless I’d prefer one of Ruby’s
standard library function, if there’s one which behaves in a similar
way. Is there one?
Regards
Thomas
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.