Jeremy Bopp wrote in post #988377:
> On 03/19/2011 11:43 PM, Neubyr Neubyr wrote:
>>>> num1.round {|a| puts a}
>> }}}
> The round method does not use a block.  As with most methods that don't
> mention they use blocks in their documentation, the block given is
> simply ignored.
>
> In both of the cases you listed above, the output you see after calling
> the round method is the value returned by that method.  irb always
> prints the value of the last executed statement.  The blocks you gave in
> both cases were silently discarded and never executed.
>
> If you would like to play around a bit with blocks, try using the each
> method on an array:
>
> irb(main):001:0> arr = [1,2,3,4]
> => [1, 2, 3, 4]
> irb(main):002:0> arr.each { |a| puts a }
> 1
> 2
> 3
> 4
> => [1, 2, 3, 4]
> irb(main):003:0> arr.each { |a| puts "test" }
> test
> test
> test
> test
> => [1, 2, 3, 4]
>
> Note that the output generated by the blocks in both cases is displayed
> *before* the =>.  Everything following the => on its line is the value
> returned by the last statement.  In each of the 3 statements above, the
> resulting value is the array created in the first line.
>
> -Jeremy

Thanks for explaining it in detail Jeremy. That's helpful.

I had played with 'Array.each {block}' as most of the examples/book had 
pointed out. But I couldn't understand how arguments are being passed to 
blocks, so I started playing with some scalar variables (objects??). I 
had referred to ri for some documentation, but didn't pay attention to 
block-calls.

So a block is dependent on method preceding it, whose output gets passed 
as args to block?? Sorry if that's really silly question.

--
thanks,
neuby.r

-- 
Posted via http://www.ruby-forum.com/.