Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Code Blocks and Iteration
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Code Blocks and Iteration
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-03-01

    Table of Contents:
  • Code Blocks and Iteration
  • 7.1 Creating and Invoking a Block
  • 7.2 Writing a Method That Accepts a Block
  • 7.3 Binding a Block Argument to a Variable

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · This article is an excerpt from the book "Ruby Cookbook," published by O'Reilly. We...
     

    Buy this book now. 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.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT