Braces bind tighter than do…end, and in ruby 1.9 arr.collect returns
an Enumerator object, which puts prints out before the do…end block
can do its thing.
In 1.9, the second fragment returns the iterator object rather
than the capitalized word.
What’s the intent of changing the behavior?
So that you can pass around Enumerator objects, or chain enumerators
together:
Remember a block is like a hidden argument to a method(despite the
claims to the contrary). If the method doesn’t yield to the block then
the block isn’t used. After all you can write a method that takes 10
arguments, and the method can ignore 9 of the arguments if you want.
puts() is ignoring the block.
Here’s a solution I offered to an earlier post that employs an
enumerator:
well, using ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux] i
don’t get identical output from the two examples…
//A
=> Mickey
Minnie
//B
=> mickey
minnie
certainly interesting… from what i understand, braces and do/end
are not exactly the same - braces have a higher precedence and bind in a
different way. check out this post -
In other words, with braces the block binds to the collect() method, but
with do…end the block binds to the puts() method. Then collect()
yields to the block, but puts() ignores it.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.