From: Dave Thomas Date: 2003-04-25T13:31:36+09:00 Subject: Re: block.call vs. yield Chris Pine wrote: > ----- Original Message ----- > From: "Yukihiro Matsumoto" > > As a conclusion, you will have compatible behavior when you use > blk.yield (new method in 1.8) instead of blk.call. > ---------------------------- > > I'm sorry... I don't understand. Will blk.yield do the same thing that > yield did? Is blk.call going away, or will it still do something different > from yield? And finally, what is the difference between blk.call and > blk.yield? It's documented in the new ri (basically parameter passing conventions) -------------------------------------------------------------- Proc#call prc.call( [params]* ) -> anObject ------------------------------------------------------------------------ Invokes the block, setting the block's parameters to the values in params using something close to method calling semantics. Returns the value of the last expression evaluated in the block. See also Proc#yield. a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} a_proc.call(9, 1, 2, 3) #=> [9, 18, 27] a_proc[9, 1, 2, 3] #=> [9, 18, 27] a_proc = Proc.new {|a| a} a_proc.call(1,2,3) #=> [1, 2, 3] call checks the number of parameters matches that in the call. a_proc = Proc.new {|a,b| a} a_proc.call(1,2,3) produces: prog.rb:1: wrong number of arguments (3 for 2) (ArgumentError) from prog.rb:1:in `call' from prog.rb:2 ------------------------------------------------------------- Proc#yield prc.yield( [params]* ) -> anObject ------------------------------------------------------------------------ Invokes the block, setting the block's parameters to the values in params using yield semantics (closer to those of parallel assignment). Returns the value of the last expression evaluated in the block. See also Proc#call. a_proc = Proc.new {|a, *b| b.collect {|i| i*a }} a_proc.yield(9, 1, 2, 3) #=> [9, 18, 27] a_proc = Proc.new {|a| a} a_proc.yield(1,2,3) #=> [1, 2, 3] a_proc = Proc.new {|a,b| a} a_proc.yield(1,2,3) #=> 1