From: Shashank Date Date: 2002-08-06T08:19:41+09:00 Subject: block , yield and proc? I am experimenting with code using blocks, yield and proc in Ruby (1.7.2 mswin32) and discovered three different ways of expressing the same idea: Q1: Why does the following code : def tstbloc(&bloc) bloc.call 1,2,3 end tstbloc { |i| puts "got: #{i}"} # this produces the output as below tstbloc {|i,j| puts "got: #{i}"} # but this generates an error produce the following output? got: 123 C:\rbtst\tstbloc.rb:2: wrong number of arguments (3 for 2) (ArgumentError) from C:\rbtst\tstbloc.rb:6:in `call' from C:\rbtst\tstbloc.rb:2:in `tstbloc' from C:\rbtst\tstbloc.rb:6 Q2: What exactly is the semantic difference between using a bloc parameter (with &) and a yield? e.g. the above code behaves differently (and I expected otherwise :-() after rewriting it as: def tstbloc yield 10,20,30 end tstbloc { |i| puts "got: #{i}"} # this produces the output as below tstbloc {|i,j| puts "got: #{i}"} # and this works too! produces: got: 102030 got: 10 Q3: And what about using Proc? def tstproc inst = Proc.new inst.call 11,22,33 end tstproc {|i| puts "got: #{i}"} tstproc {|i,j| puts "got: #{i}"} produces: got: 112233 C:\rbtst\tstbloc.rb:2: wrong number of arguments (3 for 2) (ArgumentError) from C:\rbtst\tstbloc.rb:6:in `call' from C:\rbtst\tstbloc.rb:2:in `tstbloc' from C:\rbtst\tstbloc.rb:6 Thanks, -- Shanko