Chapter 3. Strings and Ranges

image with no caption

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 (#).

In the previous example, #{name} in a double-quoted string tells Ruby to get the value of the name variable and insert that value into the string itself. The second line of code calls the gets() method to get some user input, which is then assigned to the variable name. If the user entered Fred, the final line of code would evaluate the embedded variable, #{name}, and the string “Hello Fred” would be displayed. The 1strings.rb sample program provides a few more examples of embedded evaluation in double-quoted strings. For example, here I have created an object, ob, from a custom class, MyClass, and used embedded evaluation to display the values of its name and number attributes:

1strings.rb

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 and (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	a new line
a 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	new line
a calculation #{2*3} and method-call #{ob.ten}' )

This time, no embedded evaluation is done, so this is what is displayed:

A tab	new line
calculation #{2*3} and method-call #{ob.ten}

User-Defined String Delimiters

If, for some reason, single and double quotes aren’t convenient—for example, if your strings contain lots of quote characters and you don’t want to have to keep putting backslashes in front of them—you can also delimit strings in many other ways.

The standard alternative delimiters for double-quoted strings are %Q and / or %/ and /, while for single-quoted strings they are %q and /. Thus . . .

2strings.rb

%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	a new line
a calculation using *
 #{2*3} and a method-call #{ob.ten}* )
puts( %q!b)Here's a tab	a new line
a 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	a new line
a 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.122.127