Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 2 - 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 - Iterating Over a String


    (Page 2 of 6 )

    To get the effect you want, you may have to split strings up. Here is a list of menu items, stored in a string. They are separated by \n. The each method (or its synonymeach_line) iterates over each separate item, not just the first word in the overall string, and capitalizes it:

      "new\nopen\nclose\nprint".each { |item| puts item.capitalize }# =>
      # New
      # Open
      # Close
      # Print

    By the way, there is one othereachmethod:each_byte. It takes a string apart byte by byte, returning the decimal value for the character at each index location. Print each character as a decimal, separated by/:

      "matz".each_byte { |b| print b, "/" } # => 109/97/116/122/

    This example assumes that a character is represented by a single byte, which is not always the case. The default character set for Ruby is ASCII, whose characters may be represented by bytes. However, if you use UTF-8, characters may be represented in one to four bytes. You can change your character set from ASCII to UTF-8 by specifying$KCODE='u'at the beginning of your program.

    Convert each decimal to its character equivalent withInteger’schrmethod:

      "matz".each_byte { |b| print b.chr, "/" } # => m/a/t/z/

    Or append the output to an array—out:

      out = [] # create an empty array
      "matz".each_byte { |b| p out << b} # =>
      [109]
      [109, 97]
      [109, 97, 116]
      [109, 97, 116, 122]
      p out # => [109, 97, 116, 122]

    You’ll learn more about arrays in Chapter6 .

    downcase, upcase, and swapcase

    YOU KNOW IT CAN BE ANNOYING TO READ SOMETHING THAT IS ALL IN UPPERCASE LETTERS! It’s distracting to read. That’s one reason it’s nice that Ruby has the downcase and downcase!methods.

      "YOU KNOW IT CAN BE ANNOYING TO READ SOMETHING THAT IS IN ALL UPPERCASE LETTERS!".
      downcase # => "you know it can be annoying to read something that is all in uppercase
      letters!"

    There, that’s better. But now the first letter is lowercase, too. The grammar police will be on our case. Fix this by adding a call tocapitalizeonto the statement.

      "YOU KNOW IT CAN BE ANNOYING TO READ SOMETHING THAT IS ALL IN UPPERCASE LETTERS!".
      downcase.capitalize # => "You know it can be annoying to read something that is all
      in uppercase letters!"

    Good. That took care of it.

    What if you want to go the other way and change lowercase letters to uppercase? For example, you may want to get someone’s attention by turning warning text to all uppercase. You can do that withupcaseorupcase!.

      "warning! keyboard may be hot!".upcase # => WARNING! KEYBOARD MAY BE HOT!

    Sometimes you may want to swap uppercase letters with lowercase. Useswapcaseorswapcase!. For example, you can switch an English alphabet list that starts with lowercase first to a string that starts with uppercase first:

      "aAbBcCdDeEfFgGhHiI".swapcase # => "AaBbCcDdEeFfGgHhIi"

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