Binding a Block Argument to a Variable

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 a Proc object and bound to the block variable. You can pass it around to other methods, call it directly using call, or yield to 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 named foo. If the caller chooses to pass in a block, it will be made available as a Proc object bound to the variable foo. Since it is an optional argument, foo will be nil if no block is actually passed in. This frees you from having to call Kernel#block_given? to see whether or not you got a block.

When you call a method, you can pass in any Proc object as the code block by prefixing the appropriate variable name with an ampersand. You can even do this on a Proc object that was originally passed in as a code block to your method.

Many methods for collections, like each, select, and detect, accept code blocks. It's easy to wrap such methods when your own methods can bind a block to a variable. Here, a method called biggest finds 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 block limit times, each time passing in a random number between min and max:

	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


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net