Home arrow Ruby-on-Rails arrow Page 4 - Code Blocks and Iteration
RUBY-ON-RAILS

Code Blocks and Iteration


Code blocks can be very confusing to newcomers to Ruby, despite the fact that many computer languages have something that functions in a similar manner. This article, the first of three parts, introduces you to code blocks. It is excerpted from chapter eight of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
March 01, 2007
TABLE OF CONTENTS:
  1. · Code Blocks and Iteration
  2. · 7.1 Creating and Invoking a Block
  3. · 7.2 Writing a Method That Accepts a Block
  4. · 7.3 Binding a Block Argument to a Variable

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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
  1. Recipe 8.11, “Accepting or Passing a Variable Number of Arguments”
  2. 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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials