Home arrow Ruby-on-Rails arrow Page 3 - Strings in Ruby
RUBY-ON-RAILS

Strings in Ruby


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.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
August 07, 2008
TABLE OF CONTENTS:
  1. · Strings in Ruby
  2. · General Delimited Strings
  3. · Concatenating Strings
  4. · Accessing Strings

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials