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


    (Page 1 of 6 )

    In this conclusion to a three-part series focusing on strings in Ruby, you will learn not only how to iterate over and increment a string, but how to manage whitespace, convert a string, and more. It is excerpted from chapter four of Learning Ruby, written by Michael Fitzgerald (O'Reilly; ISBN: 0596529864). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    From a String to an Array

    Conveniently, split converts a string to an array. The first call to splitis without an argument:

      "0123456789".split # => ["0123456789"]

    That was easy, but what about splitting up all the individual values and converting them into elements? Do that with a regular expression (//) that cuts up the original string at the junction of characters.

      "0123456789".split( // ) # => ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

    In the next example, the regular expression matches a comma and a space (/, /):

      c_w = "George Jones, Conway Twitty, Lefty Frizzell, Ferlin Husky"
      # => "George Jones, Conway Twitty, Lefty Frizzell, Ferlin Husky"
      c_w.split(/, /) # => ["George Jones", "Conway Twitty", "Lefty Frizzell", "Ferlin
      Husky"]

    Case Conversion

    You can capitalize a word, sentence, or phrase with capitalize or capitalize!. (By now you should know the difference between the two.) Here is a pair of sentences that are under the influence of capitalize:

      "Ruby finally has a killer app. It's Ruby on Rails.".capitalize # => "Ruby finally
      has a killer app. it's ruby on rails."

    Notice that the second sentence is not capitalized, which doesn’t look so good. Now you can see thatcapitalizeonly capitalizes the first letter of the string, not the beginning of succeeding sentences. Plan accordingly.

    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

    - Calculating Statistics with Active Record
    - Creating Graphs with Ruby
    - Callbacks and the Active Record
    - Validation and the Active Record
    - Arrays, Associations and the Active Record
    - Associations and Dependencies with Active Re...
    - Advanced Active Record: Enhancing Your Models
    - Load Balancing Databases with Rails
    - More Advanced Database Features and Rails
    - Handling Advanced Database Features with Rai...
    - Managing Database Files with Ruby on Rails
    - Databases and Ruby on Rails
    - Updating and Deleting with the Active Record
    - Rails Active Record and CRUD Functions
    - Working with a Database: Active Record







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek