I’ve made use of strings in many of my programs so far. In fact, a string was featured in the very first program in the book. Here it is again:
puts 'hello world'
Although that first program used a string enclosed within single quotes, my second program used a string in double quotes:
print('Enter your name: ' ) name = gets() puts( "Hello #{name}" )
Double-quoted strings do more work than single-quoted strings. In particular, they have the ability to evaluate bits of themselves as though they were programming code. To have something evaluated, you need to place it between a pair of curly brackets preceded by a hash mark (#
).
class MyClass attr_accessor :name attr_accessor :number def initialize( aName, aNumber ) @name = aName @number = aNumber end def ten return 10 end end ob = MyClass.new( "James Bond", "007" ) puts( "My name is #{ob.name} and my number is #{ob.number}" )
When the final line of code executes, this is displayed:
My name is James Bond and my number is 007
A double-quoted string can also evaluate expressions such as 2*3
, bits of code such as the method-call ob.ten
(where ten
is a method name), and escape characters such as \n
and \t
(representing a newline and a tab). A single-quoted string does no such evaluation. A single-quoted string can, however, use a backslash to indicate that the next character should be used literally. This is useful when a single-quoted string contains a single-quote character, like this:
'It\'s my party'
Assuming that the method named ten
returns the value 10, you might write the following code:
puts( "A tab\ta new line\na calculation #{2*3} and method-call #{ob.ten}" )
Because this is a double-quoted string, the embedded elements are evaluated, and the following is displayed:
A tab new line calculation 6 and method-call 10
Now let’s see what happens when a single-quoted string is used:
puts( 'A tab\tnew line\na calculation #{2*3} and method-call #{ob.ten}' )
This time, no embedded evaluation is done, so this is what is displayed:
A tab\tnew line\ncalculation #{2*3} and method-call #{ob.ten}
The standard alternative delimiters for double-quoted strings are %Q
and /
or %/
and /
, while for single-quoted strings they are %q
and /
. Thus . . .
%Q/This is the same as a double-quoted string./ %/This is also the same as a double-quoted string./ %q/And this is the same as a single-quoted string/
You can even define your own string delimiters. They must be nonalphanumeric characters, and they may include nonprinting characters such as newlines or tabs as well as various characters that normally have a special meaning in Ruby such as the hash mark (#
). Your chosen character should be placed after %q
or %Q
, and you should be sure to terminate the string with the same character. If your delimiter is an opening square bracket, the corresponding closing bracket should be used at the end of the string, like this:
3strings.rb
%Q[This is a string]
You will find examples of a broad range of user-selected string delimiters in the sample program 3strings.rb. Here are two examples using an asterisk (*
) after %Q
instead of a double-quoted string and using an exclamation point (!
) after %q
instead of a single-quoted string:
puts( %Q*a)Here's a tab\ta new line\na calculation using \* #{2*3} and a method-call #{ob.ten}* ) puts( %q!b)Here's a tab\ta new line\na calculation using \* #{2*3} and a method-call #{ob.ten}! )
Here, as in the previous program, ob is a user-defined object whose method named ten
returns the integer, 10. The previous code produces the following output:
a)Here's a tab a new line a calculation using * 6 and a method-call 10 b)Here's a tab\ta new line\na calculation using \* #{2*3} and a method-call #{ob.ten}
Although there may be times when it is useful to delimit a string by some esoteric character such as a newline or an asterisk, in many cases the disadvantages (not least the mental anguish and confusion) resulting from such arcane practices may significantly outweigh the advantages.