Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow 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 
Sun Developer Network 
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

Strings in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-08-07

    Table of Contents:
  • Strings in Ruby
  • General Delimited Strings
  • Concatenating Strings
  • Accessing Strings

  • 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


    Strings in Ruby


    (Page 1 of 4 )

    Strings are used in every programming language, and most programmers spend a lot of time manipulating strings. This article will explain the various ways you can use and manipulate strings in Ruby. It is excerpted from chapter four of Learning Ruby, written by Michael Fitzgerald (O'Reilly, 2007; 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.

    In the simplest terms, a string in a programming language is a sequence of one or more characters and usually represents some human language, whether written or spoken. You are probably more likely to use methods from the String class than from any other class in Ruby. Manipulating strings is one of the biggest chores a programmer has to manage. Fortunately, Ruby offers a lot of convenience in this department.

    For more information on string methods, go to http://www.ruby-doc.org/core/classes/String.html. You can also use the command line to get information on a method. For example, to get information on theStringinstance methodchop, type:

      ri String#chop [or] ri String.chop

    You can use#or.between the class and method names when returning two methods with ri. This, of course, assumes that you have the Ruby documentation package installed and that it is in the path (see “Installing Ruby,” in Chapter1 ).

    Creating Strings

    You can create strings with the newmethod. For example, this line creates a new, empty string calledtitle:

      title = String.new # => ""

    Now you have a new string, but it is only filled with virtual air. You can test a string to see if it is empty withempty?:

      title.empty? # => true

    You might want to test a string to see if it is empty before you process it, or to end processing when you run into an empty string. You can also test its length or size:

      title.length [or] title.size # => 0

    Thelengthandsizemethods do the same thing: they both return an integer indicating how many characters a string holds.

    Thenewmethod can take a string argument:

      title = String.new( "Much Ado about Nothing" )

    Now checktitle:

      title.empty? # => false
      title.length # => 22

    There we go. Not quite so vacuous as before.

    Another way to create a string is withKernel’sString method:

      title = String( "Much Ado about Nothing" )
     
    puts title # => Much Ado about Nothing

    But there is an even easier way. You don’t have to use theneworStringmethods to generate a new string. Just an assignment operator and a pair of double quotes will do fine:

      sad_love_story = "Romeo and Juliet"

    You can also use single quotes:

      sad_love_story = 'Romeo and Juliet'

    The difference between using double quotes versus single quotes is that double quotes interpret escaped characters and single quotes preserve them. I’ll show you what that means. Here’s what you get with double quotes (interprets\nas a newline):

      lear = "King Lear\nA Tragedy\nby William Shakespeare"
      puts lear # => King Lear
                #    A Tragedy
               
    #    by William Shakespeare

    And here’s what you get with single quotes (preserves\nin context):

      lear = 'King Lear\nA Tragedy\nby William Shakespeare'
      puts lear # => King Lear\nA Tragedy\nby William Shakespeare

    For a complete list of escape characters, see Table A-1 in Appendix A.

    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, 2007; 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-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT