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


    (Page 3 of 4 )

    In Ruby, you can add on to an existing string with various concatenation techniques. With Ruby, you don’t have to jump through the hoops that you might if you were using a language with immutable strings.

    Adjacent strings can be concatenated simply because that they are next to each other:

      "Hello," " " "Matz" "!" # => "Hello, Matz!"

    You can also use the+ method:

      "Hello," + " " + "Matz" + "!" # => "Hello, Matz!"

    You can even mix double and single quotes, as long as they are properly paired.

    Another way to do this is with the<<method. You can add a single string:

      "Hello, " << "Matz!" # => Hello, Matz!

    Or you can chain them together with multiple calls to<<:

      "Hello," << " " << "Matz" << "!" # => Hello, Matz!

    An alternative to<<is theconcatmethod (which does not allow you to chain):

      "Hello, ".concat "Matz!"

    Or you can do it this way:

      h = "Hello,"
      m = "Matz!"
      h.concat(m)

    You can make a string immutable withObject’sfreezemethod:

      greet = "Hello, Matz!"
      greet.
    freeze

      # try to append something
      greet.concat("!") # => TypeError: can't modify frozen string

      # is the object frozen?
      greet.frozen? # => true

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek