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:intstbloc’
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:intstbloc’
from C:\rbtst\tstbloc.rb:6

Thanks,
– Shanko

Hi,

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:

First, you have to learn several tips:

(a) block parameter |v| means takes everything in a single variable,
which is better described as |*v|.

(b) “call” requires exact number of required argument, whereas
“yield” fills nil for fewer arguments and ignores too much
arguments.

Q1: Why does the following code :

This is caused by (b).

Q2: What exactly is the semantic difference between using a bloc parameter
(with &) and a yield?

This is answered almost by (b). In addition, a block argument
(&block) creates one more object.

Q3: And what about using Proc?

No difference, except for appearance.

						matz.
···

In message “block , yield and proc?” on 02/08/06, “Shashank Date” ADATE@kc.rr.com writes:

Oh, OK ! Got it …
Thanks Matz.

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1028615833.651199.10281.nullmailer@picachu.netlab.jp…

Hi,

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:

First, you have to learn several tips:

(a) block parameter |v| means takes everything in a single variable,
which is better described as |*v|.

(b) “call” requires exact number of required argument, whereas
“yield” fills nil for fewer arguments and ignores too much
arguments.

Q1: Why does the following code :

This is caused by (b).

Q2: What exactly is the semantic difference between using a bloc
parameter

···

In message “block , yield and proc?” > on 02/08/06, “Shashank Date” ADATE@kc.rr.com writes:

(with &) and a yield?

This is answered almost by (b). In addition, a block argument
(&block) creates one more object.

Q3: And what about using Proc?

No difference, except for appearance.

matz.