On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk <joe.vandyk / boeing.com> wrote:
> When do you use the 'yield' statement in code?

When you want to write methods that take blocks.

| def repeatedly_write(times, &block)
|   times.times do |i|
|     puts yield(i)
|   end
| end
|
| repeatedly_write(5) do |i|
|   "iteration #{i}"
| end

prints:
iteration 0
iteration 1
iteration 2
iteration 3
iteration 4

In this example, ``yield(i)'' and ``block.call(i)'' are equivalent.

> Joe

Sam