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
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
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
You can use
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).
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
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:
[or]title.size # => 0
size methods do the same thing: they both return an integer indicating how many
characters a string holds.
new method can take a string argument:
title = String.new( "Much Ado about Nothing" )
title.empty? # => false title.length # => 22
There we go. Not quite so vacuous as before.
Another way to create a string is with
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
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"
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
\nby William Shakespeare" puts lear # => King Lear # A Tragedy # by William Shakespeare
lear = 'King Lear
\nby William Shakespeare' puts lear # => King Lear
\nby William Shakespeare
For a complete list of escape characters, see Table A-1 in Appendix A.
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
[ (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
%q, which is equivalent to a
single-quoted string; or
%x for a back-quoted string
(`) for command output.
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. 29
This document is stored in the string
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:
You'll see for yourself how the lines break.
You can also "delimit the delimiter" for various effects:
<<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