Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Iterating and Incrementing Strings 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

Iterating and Incrementing Strings in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-08-21

    Table of Contents:
  • Iterating and Incrementing Strings in Ruby
  • Iterating Over a String
  • Managing Whitespace, etc.
  • Incrementing Strings
  • Converting Strings
  • Regular Expressions

  • 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


    Iterating and Incrementing Strings in Ruby - Incrementing Strings


    (Page 4 of 6 )

    The Ruby String class has several methods that let you produce successive strings—that is, strings that increment, starting at the rightmost character. You can increment strings with next and next!(orsuccandsucc!). I prefer to usenext. (The methods ending in!make in-place changes.) For example:

      "a".next [or] "a".succ # => "b"

    Remember,nextincrements the rightmost character:

      "aa".next # => "ab"

    It adds a character when it reaches a boundary, or adds a digit or decimal place when appropriate, as shown in these lines:

      "z".next # => "aa" # two a's after onez
      "zzzz".next # => "aaaaa" # five a's after four z's
      "999.0".next # => "999.1" # increment by .1
      "999".next # => "1000" # increment from 999 to 1000

    We’re not just talking letters here, but any character, based on the character set in use (ASCII in these examples):

      " ".next # => "!"

    Chain calls ofnexttogether—let’s try three:

      "0".next.next.next # => "3"

    As you saw earlier,nextworks for numbers represented as strings as well:

      "2007".next # => "2008"

    Or you can get it to work when numbers are not represented as strings, though the method will come from a different class, notString. For example:

      2008.next # => 2009

    Instead of fromString, this call actually uses thenextmethod fromInteger. (TheDate,Generator,Integer, andString classes all havenext methods.)

    You can even use a character code viachrwithnext:

      120.chr # => "x"
      120.chr.next # => "y"

    Theuptomethod fromString, which uses a block, makes it easy to increment. For example, this call touptoprints the English alphabet:

      "a".upto("z") { |i| print i } # => abcdefghijklmnopqrstuvwxyz

    You could also do this with aforloop and an inclusive range:

      for i in "a".."z"
        print i
      end

    You decide what’s simpler. Theforloop takes only slightly more keystrokes (29 versus 31, including whitespace). But I likeupto.

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


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

    Buy this book now. This article is excerpted from chapter four of Learning Ruby, written by Michael Fitzgerald (O'Reilly; ISBN: 0596529864). 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