Code Blocks and Iteration - 7.3 Binding a Block Argument to a Variable
(Page 4 of 4 )
Problem
You’ve written a method that takes a code block, but it’s not enough for you to simply call the block with yield. You need to somehow bind the code block to a variable, so you can manipulate the block directly. Most likely, you need to pass it as the code block to another method.
Solution Put the name of the block variable at the end of the list of your method’s arguments. Prefix it with an ampersand so that Ruby knows it’s a block argument, not a regular argument.
An incoming code block will be converted into aProc object and bound to the block variable. You can pass it around to other methods, call it directly usingcall, oryieldto it as though you’d never bound it to a variable at all. All three of the following methods do exactly the same thing:
def repeat(n)
n.times { yield } if block_given?
end
repeat(2) { puts "Hello." }
# Hello.
# Hello.
def repeat(n, &block)
n.times { block.call } if block
end
repeat(2) { puts "Hello." }
# Hello.
# Hello.
def repeat(n, &block)
n.times { yield } if block
end
repeat(2) { puts "Hello." }
# Hello.
# Hello.
Discussion If &foo is the name of a method’s last argument, it means that the method accepts an optional block namedfoo. If the caller chooses to pass in a block, it will be made available as aProc object bound to the variablefoo. Since it is an optional argument,foowill benilif no block is actually passed in. This frees you from having to callKernel#block_given?to see whether or not you got a block.
When you call a method, you can pass in anyProcobject as the code block by prefixing the appropriate variable name with an ampersand. You can even do this on aProcobject that was originally passed in as a code block to your method.
Many methods for collections, likeeach,select, anddetect, accept code blocks. It’s easy to wrap such methods when your own methods can bind a block to a variable. Here, a method calledbiggestfinds the largest element of a collection that gives a true result for the given block:
def biggest(collection, &block)
block ? collection.select(&block).max : collection.max
end
array = [1, 2, 3, 4, 5]
biggest(array) {|i| i < 3} # => 2
biggest(array) {|i| i != 5 } # => 4
biggest(array) # => 5
This is also very useful when you need to write a frontend to a method that takes a block. Your wrapper method can bind an incoming code block to a variable, then pass it as a code block to the other method.
This code calls a code blocklimittimes, each time passing in a random number betweenminandmax:
def pick_random_numbers(min, max, limit)
limit.times { yield min+rand(max+1) }
end
This code is a wrapper method for pick_random_numbers. It calls a code block 6 times, each time with a random number from 1 to 49:
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
The code block argument must always be the very last argument defined for a method. This means that if your method takes a variable number of arguments, the code block argument goes after the container for the variable arguments:
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
See Also - Recipe 8.11, “Accepting or Passing a Variable Number of Arguments”
- Recall from the chapter introduction that in Ruby 1.8, a code block cannot itself take a block argument; this is fixed in Ruby 1.9
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter seven of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Check it out today at your favorite bookstore. Buy this book now.
|
|