Home arrow Ruby-on-Rails arrow Iterating and Incrementing Strings in Ruby

Iterating and Incrementing Strings in Ruby

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.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
August 21, 2008
  1. · Iterating and Incrementing Strings in Ruby
  2. · Iterating Over a String
  3. · Managing Whitespace, etc.
  4. · Incrementing Strings
  5. · Converting Strings
  6. · Regular Expressions

print this article

Iterating and Incrementing Strings in Ruby
(Page 1 of 6 )

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

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.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials