Comparing and Manipulating Strings in Ruby - The delete Method
(Page 4 of 4 )
With delete or delete!, you can delete characters from a string:
"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 usedeletethe way I want, so how do I fixcalll? 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 becausedeleteuses 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 expressions:
"That's all folks".delete "abcdefghijklmnopqrstuvwxyz", "^ha" # => "haa"
The caret negates both the characters in the argument, not just the first one (you can do"^h^a", too, and get the same answer).
Substitute the Substring
Try gsub (or 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"
Thereplacemethod 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 usereplace,callremains the same object, with the same object ID, but when you assign the string tocalltwice, the object and its ID will change. Just a subtlety you ought to know.
# same object
call = "All hands on deck!" # => "All hands on deck!"
call.object_id # => 1624370
call.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 # => 1600420
call = "All feet on deck!" # => "All feet on deck!"
call.object_id # => 1009410
Turn It Around
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:
"abcdefghijklmnopqrstuvwxyz".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 thoughreverse!changed the string in place. Think about that one for a while.
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
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.
|
|