Home arrow Ruby-on-Rails arrow Strings in Ruby

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 / 6
August 07, 2008
  1. · Strings in Ruby
  2. · General Delimited Strings
  3. · Concatenating Strings
  4. · Accessing Strings

print this article

Strings in Ruby
(Page 1 of 4 )

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.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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