Example uses of 'yield'
A block is enclosed in braces, {...}, or do...end, and may have formal parameters enclosed by pipes ("|"). A block is passed to a method as an "extra parameter" and the method executes the block by executing the statement yield with the correct number of actual parameters.
This is called a closure because the context in which the block is written (including such things as the values of local variables, whether it is in a loop, etc.) is "sealed up" and passed along as part of the block.
| Page 50 in textbook: Simplest use of 'yield' |
def three_times
puts "---------- three_times"
yield
yield
yield
end
|
three_times { puts "hello" } |
---------- three_times
hello
hello
hello
|
| Modified version of above which yields values |
def count_to_3
puts "---------- count_to_3"
yield 1
yield 2
yield 3
end
|
count_to_3 { |result| puts result } |
---------- count_to_3
1
2
3
|
| Closure which takes a parameter |
def count_to n
puts "---------- count_to #{n}"
for i in 1..n
yield
end
end
|
count_to(3) { puts "no result" } |
---------- count_to 3
no result
no result
no result
|
| Closures also return results |
def twice n
puts "---------- twice #{n}"
yield 2 * n
99
end
|
p = twice(7) { |y| puts "y = #{y}" }
puts "p = #{p}"
|
---------- twice 7
y = 14
p = 99
|
| Closure which takes a parameter and yields values |
def n_times n
puts "---------- n_times #{n}"
for i in 1..n
yield 10 * i
end
end
|
n_times 3 do |result|
puts result
end
|
n_times(3) { |result|
# \_\_ Parentheses required here
puts result
}
|
---------- n_times 3
10
20
30
|
(Same output for both)
| Closures, like methods, may yield multiple values |
def squares_and_cubes
puts "---------- squares_and_cubes"
for i in 2..5
yield i, i * i, i * i * i
end
end
|
p = squares_and_cubes do |x, y, z|
puts "#{x}, #{y}, #{z}"
end
|
---------- squares_and_cubes
2, 4, 8
3, 9, 27
4, 16, 64
5, 25, 125
|
puts "p = #{p} |
p = 2..5 |
| Closures can take parameters and yield multiple values |
def div_and_mod x, y
puts "---------- f div_and_mod #{x}, #{y}"
yield x / y, x % y
end
|
div_and_mod 20, 3 do |div, mod|
puts "div = #{div}, mod = #{mod}"
end
|
---------- f div_and_mod 20, 3
div = 6, mod = 2
|
| My own version of 'loop' |
def my_loop
puts "---------- my_loop"
yield while true
end
|
c = 0
my_loop do
puts c += 1
break if c > 2
end
|
---------- my_loop
1
2
3
|
| Iterator for characters in a String |
class String
def each_chr
puts "---------- each_chr"
for i in 0..self.length - 1
yield self[i].chr
end
end
end
|
"abc".each_chr { |v| puts v } |
---------- each_chr
a
b
c
|
| Fibonacci numbers less than n |
def fib_to_n n
puts "---------- fib_to_n #{n}"
i1, i2 = 1, 1
while i1 < n
yield i1
i1, i2 = i2, i1 + i2
end
end
|
fib_to_n(10) { |f| puts f } |
---------- fib_to_n 10
1
1
2
3
5
8
|
| Loop from Range#min to Range#max by steps of n |
class Range
def my_step(n)
puts "---------- (#{self}).my_step #{n}"
count = -1
self.each do |this|
count += 1
yield this if count % n == 0
end
end
end
|
(7..20).my_step(3) { |n| puts n } |
---------- (7..20).my_step 3
7
10
13
16
19
|