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"
Next: Managing Whitespace, etc. >>
More Ruby-on-Rails Articles
More By O'Reilly Media
|
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.
|
|