On 03/19/2011 11:43 PM, Neubyr Neubyr wrote: > I am learning Ruby and having some difficulty understanding blocks. I > was trying out following code for getting better idea about blocks. I > was expecting case 2 to print 'test' string, but it is not printing > out anything. Any elaboration on what's happening here would be really > helpful. > > case 1: > {{{ >>> num1 = 1.22 > => 1.22 >>> num1.round {|a| puts a} > => 1 > }}} > > case 2: > {{{ >>> num1 = 1.22 > => 1.22 >>> num1.round {|a| puts "test"} > => 1 >>> > }}} 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