Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - 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 chomp and chop Methods


    (Page 3 of 4 )

    The chop (or chop!) method chops off the last character of a string, and thechomp(chomp!) method chomps off the record separator ($/)—usually just a newline—from a string. Consider the stringjoe, a 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"

    Applychomp!to remove the last record separator (\n):

      joe.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, andchomp!returnsnilwithout altering the string because there is no record separator at the end of the string:

      joe.chomp! # => nil

    chop,chomp’s greedy twin, shows no mercy on the string, removing the last character (a quote) with abandon:

      joe.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"

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · 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 4 Hosted by Hostway
    Stay green...Green IT