This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Binding a Block Argument to a Variable
CodeExpectedActual
def repeat(n)
  n.times { yield } if block_given?
end
repeat(2) { puts "Hello." }
Hello.
Hello.
Hello.
Hello.
def repeat(n, &block)
  n.times { block.call } if block
end
repeat(2) { puts "Hello." }
Hello.
Hello.
Hello.
Hello.
def repeat(n, &block)
  n.times { yield } if block
end
repeat(2) { puts "Hello." }
Hello.
Hello.
Hello.
Hello.
def biggest(collection, &block)
  block ? collection.select(&block).max : collection.max
end
array = [1, 2, 3, 4, 5]
biggest(array) {|i| i < 3}
2 2
biggest(array) {|i| i != 5 }
4 4
biggest(array)
5 5
def pick_random_numbers(min, max, limit)
  limit.times { yield min+rand(max+1) }
end
def lottery_style_numbers(&block)
  pick_random_numbers(1, 49, 6, &block)
end
lottery_style_numbers { |n| puts "Lucky number: #{n}" }
Lucky number: 20
Lucky number: 39
Lucky number: 41
Lucky number: 10
Lucky number: 41
Lucky number: 32
Lucky number: 20
Lucky number: 39
Lucky number: 41
Lucky number: 10
Lucky number: 41
Lucky number: 32
def invoke_on_each(*args, &block)
  args.each { |arg| yield arg }
end
invoke_on_each(1, 2, 3, 4) { |x| puts x ** 2 }
1
4
9
16
1
4
9
16