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

Strings in Ruby
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    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 - General Delimited Strings


    (Page 2 of 4 )

    Another way to create strings is with general delimited strings, which are all preceded by a % and then followed by a matched pair of delimiter characters, such as!,{, or[(must be nonalphanumeric). The string is embedded between the delimiters. All of the following examples are delimited by different characters (you can even use quote characters):

      comedy = %!As You Like It!
      history = %[Henry V]
      tragedy = %(Julius Ceasar)

    You can also use%Q, which is the equivalent of a double-quoted string;%q, which is equivalent to a single-quoted string; or%xfor a back-quoted string (`) for command output.

    Here Documents

    A here document allows you to build strings from multiple lines on the fly, while preserving newlines. A here document is formed with a<< and a delimiting character or string of your choice. I’ll save Shakespeare’s 29th sonnet as a here document, with29as the delimiter:

      sonnet = <29
      When in disgrace with fortune and men's eyes
      I all alone beweep my outcast state,
      And trouble deaf heaven with my bootless cries,
      And look upon myself, and curse my fate,
      Wishing me like to one more rich in hope,
      Featured like him, like him with friends possessed,
      Desiring this man's art, and that man's scope,
      With what I most enjoy contented least;
      Yet in these thoughts my self almost despising,
      Haply I think on thee, and then my state,
      Like to the lark at break of day arising
      From sullen earth, sings hymns at heaven's gate;
      For thy sweet love remembered such wealth brings
      That then I scorn to change my state with kings.
     
    29

    This document is stored in the string sonnet, but you can create a here document without placing it in a string. Wherever the line breaks, a record separator (such as\n) is inserted at that place. Now use:

      puts sonnet

    You’ll see for yourself how the lines break.

    You can also “delimit the delimiter” for various effects:

      sonnet = <<hamlet # same as double-quoted string
      O my prophetic soul! My uncle!
      hamlet

      sonnet = <<"hamlet" # again as double-quoted string
      O my prophetic soul! My uncle!
      hamlet

      sonnet = <<'ghost' # same as single-quoted string
      Pity me not, but lend thy serious hearing
      To what I shall unfold.
     
    ghost

      my_dir = <<`dir` # same as back ticks
      ls -l
      
    dir

      ind = <<-hello # for indentation
          Hello, Matz!
      hello

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT