"A horse! "
* 2# => "A horse! A horse! "
You can concatenate a string to the result:
taf = "That's ".downcase * 3 + "all folks!" # => "that's that's that's all folks!" taf.capitalize # => "That's that's that's all folks!"
insert method lets you insert another string at
a given index in a string. For example, you can correct spelling:
insert 6, "e"# => "Be careful."
or add a word (plus a space):
insert 3, "very "# => "Be very careful!"
or even throw the
* method in just to prove that
insert 3, "very " * 5# => "Be very very very very very careful!"
You can alter all or part of a string, in place, with the
= method. (Like
, which is the
= is an alias of
slice!, so anywhere you
=, you can use
slice!, with the same arguments.)
Given the following strings (some scoundrel has been editing our Shakespeare text):
line = "A Porsche! a Porsche! my kingdom for a Porsche!" cite = "Act V, Scene V" speaker = "King Richard, 2007"
enter a string as the argument to
=, and it will
return the new, corrected string, if found;
speaker[", 2007"]= "III" # => "III" p speaker # => "King Richard III"
That's looking better.
If you specify a
Fixnum (integer) as an index, it
returns the corrected string you placed at the index location. (String lengths are
automatically adjusted by Ruby if the replacement string is a different length than the
cite= "IV" # => "IV" p cite # => "Act V, Scene IV"
At the index
= found the substring
V and replaced it
You can use an offset and length (two
= the index of the substring where you want to
start, and then how many characters you want to retrieve:
line[39,8]= "Porsche 911 Turbo!" # => "Porsche 911 Turbo!" p line # => "A Porsche! a Porsche! my kingdom for a Porsche 911 Turbo!"
You started at index
39, and went
8 characters from there (inclusive).
You can also enter a range to indicate a range of characters you want to change.
Include the last character with two dots (
speaker[13..15]= "the Third" # => "the Third" p speaker # => "King Richard the Third"
You can also use regular expressions (see "Regular Expressions," later in this chapter), as shown here:
line[/Porsche!$/]= "Targa!" # => "Targa!" p line # => "A Porsche! a Porsche! my kingdom for a Targa!"
The regular expression
/Porsche!$/ matches if
Porsche! appears at the end of the line (
$). If this is true, the call to
chop!) method chops off the last character of a string, and the
chomps off the record separator (
$/)—usually just a
newline—from a string. Consider the string
limerick created as a here document:
joe = <<limerick There once was a fellow named Joe quite fond of Edgar Allen Poe He read with delight Nearly half the night When his wife said "Get up!" he said "No." limerick # => "There once was a fellow named Joe\nquite fond of Edgar Allen Poe\n He read with delight\n Nearly half the night\nWhen his wife said \"Get up!\" he said \"No.\"\n"
chomp! to remove the last record separator
chomp!# => "There once was a fellow named Joe\nquite fond of Edgar Allen Poe\n He read with delight\n Nearly half the night\nWhen his wife said \"Get up!\" he said \"No.\""
Now apply it again, and
nil without altering the string because there is no record
separator at the end of the string:
joe.chomp! # => nil
greedy twin, shows no mercy on the string, removing the last character (a quote) with
chop!= "There once was a fellow named Joe\nquite fond of Edgar Allen Poe\n He read with delight\n Nearly half the night\nWhen his wife said \"Get up!\" he said \"No"
"That's call folks!".
delete "c"# => "That's all folks"
That looks easy, because there is only one occurrence of the letter c in the string, so you don't see any interesting side effects, as you would in the next example. Let's say you want to get rid of that extra l in alll:
"That's alll folks".
delete "l"# => "That's a foks"
Oh, boy. It cleaned me out of all ls. I can't use
delete the way I want, so how do I fix
calll? What if I use two ls instead of one?
"That's alll folks".
delete "ll"# => "That's a foks"
I got the same thing. (I knew I would.) That's because
delete uses the intersection (what intersects or is the
same in both) of its arguments to decide what part of the string to take out. The nifty
thing about this, though, is you can also negate all or part of an argument with the caret
^), similar to its use in regular
"That's all folks".delete "abcdefghijklmnopqrstuvwxyz", "^ha" # => "haa"
The caret negates both the characters in the argument, not just the first one (you can
"^h^a", too, and get the same answer).
gsub!). This method replaces a substring (first argument) with a replacement
string (second argument):
"That's alll folks".
gsub "alll", "all"# => "That's all folks"
Or you might do it this way:
"That's alll folks".
gsub "lll", "ll"# => "That's all folks"
replace method replaces a string wholesale. Not
just a substring, the whole thing.
call = "All hands on deck!" call.replace "All feet on deck!" # => "All feet on deck!"
So why wouldn't you just do it this way?
call = "All hands on deck!" call = "All feet on deck!"
Wouldn't you get the same result? Not exactly. When you use
call remains the same object,
with the same object ID, but when you assign the string to
call twice, the object and its ID will change. Just a subtlety you ought to
# same object call = "All hands on deck!" # => "All hands on deck!" call.object_id # =>
1624370call.replace "All feet on deck!" # => "All feet on deck!" call.object_id # =>
1624370# different object call = "All hands on deck!" # => "All hands on deck!" call.object_id # =>
1600420call = "All feet on deck!" # => "All feet on deck!" call.object_id # =>
To reverse the characters means to alter the characters so they read in the opposite
direction. You can do this with the
reverse method (or
reverse! for permanent damage). Say you want to
reverse the order of the English alphabet:
reverse# => "zyxwvutsrqponmlkjihgfedcba"
Or, maybe you'd like to reverse a palindrome:
palindrome = "dennis sinned" palindrome.
reverse!# => "dennis sinned" p palindrome
Not much harm done, even though
the string in place. Think about that one for a while.
split converts a string to an array.
The first call to
split is without an argument:
.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 Husky"]