Home arrow Ruby-on-Rails arrow Page 4 - Comparing and Manipulating Strings in Ruby
RUBY-ON-RAILS

Comparing and Manipulating Strings in Ruby


In this second part of a three-part series focusing on strings in Ruby, you will learn how to test two strings to see if they are the same, 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: 4 stars4 stars4 stars4 stars4 stars / 7
August 14, 2008
TABLE OF CONTENTS:
  1. · Comparing and Manipulating Strings in Ruby
  2. · Manipulating Strings
  3. · The chomp and chop Methods
  4. · The delete Method

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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