Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Iterators in Ruby
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

Iterators in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-03-08

    Table of Contents:
  • Iterators in Ruby
  • 7.5 Writing an Iterator Over a Data Structure
  • 7.6 Changing the Way an Object Iterates
  • 7.7 Writing Block Methods That Classify or Collect

  • 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


    Iterators in Ruby


    (Page 1 of 4 )

    In this second part of a three-part article, we continue our discussion of code blocks and begin focusing on iterators as well. 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.

    7.4 Blocks as Closures: Using Outside Variables Within a Code Block

    Problem

    You want to share variables between a method, and a code block defined within it.

    Solution

    Just reference the variables, and Ruby will do the right thing. Here’s a method that adds a certain number to every element of an array:

      def add_to_all(array, number)
        array.collect { |x| x + number }
      end

      add_to_all([1, 2, 3], 10)   # => [11, 12, 13]

    Enumerable#collectcan’t accessnumberdirectly, but it’s passed a block that can access it, sincenumberwas in scope when the block was defined.

    Discussion

    A Ruby block is a closure: it carries around the context in which it was defined. This is useful because it lets you define a block as though it were part of your normal code, then tear it off and send it to a predefined piece of code for processing.

    A Ruby block contains references to the variable bindings, not copies of the values. If the variable changes later, the block will have access to the new value:

      tax_percent = 6
      position = lambda do
       
    "I have always supported a #{tax_percent}% tax on imported limes."
      end
      position.call
      # => "I have always supported a 6% tax on imported limes."

      tax_percent = 7.25
      position.call
      # => "I have always supported a 7.25% tax on imported limes."

    This works both ways: you can rebind or modify a variable from within a block.

      counter = 0
      4.times { counter += 1; puts "Counter now #{counter}"}
      # Counter now 1
      # Counter now 2
      # Counter now 3
      # Counter now 4
      counter                            # => 4

    This is especially useful when you want to simulateinjectorcollect in conjunction with a strange iterator. You can create a storage object outside the block, and add things to it from within the block. This code simulatesEnumerable#collect, but it collects the elements of an array in reverse order:

      accumulator = []
      [1, 2, 3].reverse_each { |x| accumulator << x + 1 }

      accumulator               # => [4, 3, 2]

    Theaccumulatorvariable is not within the scope ofArray#reverse_each, but it is within the scope of the block.

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · 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 eight 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek