data = [1, 2, 3]
data.each do |i|
print i, "\n"
end
This script yields this output.
$ ruby test.rb
1
2
3
That is, the block between do
and end
is repeatedly executed
on each element of an array "data".
int data[3] = {1, 2, 3};
int i;
for (i = 0; i < 3; i++) {
printf("%d\n", data[i]);
}
You must be very careful on the boundary condition when using "for".
You are free from such bugs with an iterator.
do
...end
, you can use {
...}
.
data = [1, 2, 3]
data.each { |i|
print i, "\n"
}
This code has the same meaning as the last example. In some cases
do
...end
and {
...}
act differently.
foobar a, b do .. end # foobar is the iterator.
foobar a, b { .. } # b is the iterator.
This is because { }
attaches stronger to the preceding expression
than a do
block.
yield
control structure, a block argument, and Proc.new
.
( rb_yield
is used in C extension library.)
yield
statement may have arguments which are passed to the
block as block parameters and the block is executed.
method.call(args...)
form.
Proc.new
, when used in an iterator definition, takes the block which
is given to the method as
its argument, generates a procedure object generated from the block. The
same applies to proc
and lambda
.
def a (&b)
yield
b.call
Proc.new.call
proc.call
lambda.call
end
a{print "test\n"}
Proc.new
without a block do?
Proc.new
without a block cannot generate a procedure object and an
error occurs. In a method definition, Proc.new
without a block
implies the existence of a block given by the method call.