O'Reilly logo

Learning Ruby by Michael Fitzgerald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Strings

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 the String instance method chop, 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 Chapter 1).

Creating Strings

You can create strings with the new method. For example, this line creates a new, empty string called title:

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 with empty?:

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

The length and size methods do the same thing: they both return an integer indicating how many characters a string holds.

The new method can take a string argument:

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

Now check title:

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

There we go. Not quite so vacuous as before.

Another way to create a string is with Kernel's String 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 the new or String methods 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 \n as 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 \n in 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.

General Delimited Strings

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 %x for 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, with 29 as 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.

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!

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

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

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

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required