Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 4 - Comparing and Manipulating Strings in Ruby
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Comparing and Manipulating Strings in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-08-14

    Table of Contents:
  • Comparing and Manipulating Strings in Ruby
  • Manipulating Strings
  • The chomp and chop Methods
  • The delete Method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 an excerpt from the book "Learning Ruby," published by O'Reilly. We...
     

    Buy this book now. 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.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT