Home arrow Ruby-on-Rails arrow Page 3 - 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 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"


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