Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - 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 - Managing Whitespace, etc.


    (Page 3 of 6 )

     

    You can adjust whitespace (or other characters) on the left or right of a string, center a string in whitespace (or other characters), and strip whitespace away using the following methods. First, create a string—the title of a Shakespeare play:

      title = "Love's Labours Lost"

    How long is the string? This will be important to you (lengthandsizeare synonyms).

      title.size # => 19

    The stringtitleis 19 characters long. With that information in tow, we can start making some changes. Theljustandrjustmethods pad a string with whitespace or, if specified, some other character. The string will be right justified, and the number of characters, whitespace or otherwise, must be greater than the length of the string. Make sense? I hope so. Let’s go over an example or two.

    Let’s call these two methods with an argument (an integer) that is less than or equal to the length of the string.

      title.ljust 10 # => "Love's Labours Lost"
      title.rjust 19 # => "Love's Labours Lost"

    What happened? Nothing! That’s because the argument must be greater than the length of the string in order to do anything. The added whitespace is calculated based on the length of the string plus the value of the argument. Watch:

      title.ljust 20 # => "Love's Labours Lost"
      title.rjust 25 # => "      Love's Labours Lost"

    See how it works now? In the call toljust, one space character is added on the right (20 – 19 = 1), and the call torjustadds six characters to the left (25 – 19 = 6). If it seems backward, just remember that the string is always right justified. Still confused? So am I, but we’ll go on. You can use another character besides the default space character if you’d like:

      title.rjust( 21, "-" ) # => "--Love's Labours Lost"

    or use more than one character—the sequence will be repeated:

      title.rjust 25, "->" # => "->->->Love's Labours Lost"

    OK, now let’s really mess with your head:

      title.rjust(20, "-").ljust(21, "-") # => "-Love's Labours Lost-"

    You might want to do something like that someday.

    If you want to play both ends to the middle, we are better off usingcenterinstead:

      title.center 23 # => " Love's Labours Lost "
      title.center 23, "-" # => "--Love's Labours Lost--"

    With one more tip of the hat, I’ll usecenter to create a comment:

      filename = "hack.rb" # => "hack.rb"
      filename.size # => 7
      filename.center 40-7, "#" # => "#############hack.rb#############"

    We’ve been adding whitespace and other characters. What if you just want to get rid of it? Uselstrip,rstrip, andstrip(lstrip!,rstrip!, andstrip!). Suppose you have a string surrounded by whitespace:

      fear = "      Fear is the little darkroom where negatives develope. --Michael
      Pritchard           "

    Oops. Fell asleep with my thumb on the space bar—twice! I can fix it easily now, starting with the left side (make the change stick to the original string withlstrip!):

      fear.lstrip! # => "Fear is the little darkroom where negatives develope. -- Michael
      Pritchard                   "

    Now the right side:

      fear.rstrip! # => "Fear is the little darkroom where negatives develope. -- Michael
      Pritchard"

    Or do the whole thing at once:

      fear.strip! # => "Fear is the little darkroom where negatives develope. -- Michael
      Pritchard"

    strip removes other kinds of whitespace, too:

      "\t\tBye, tabs and line endings!\r\n".strip # => "Bye, tabs and line endings!"

    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 5 Hosted by Hostway
    Stay green...Green IT