Home arrow Ruby-on-Rails arrow Page 2 - Iterating and Incrementing Strings in Ruby
RUBY-ON-RAILS

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 / 3
August 21, 2008
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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"


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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