3.2. Hello, Ruby!

You have to start somewhere, so I won't break the tradition of using a Hello world program as the first example.

Place the following lines into a file called hello.rb as shown in Listing 3-1.

Example 3.1. Hello world in Ruby
# Saying hello the Ruby way
puts 'Hello, Ruby!'

To run this, execute ruby hello.rb from the command line. Not surprisingly, Ruby displays the string Hello, Ruby! as shown in Figure 3-1.

Figure 3.1. Figure 3-1

When working with single Ruby files on Windows, it is convenient to use the SciTE editor (shown in Figure 3-2 that gets installed with the One-Click Ruby Installer. By default, this is located in c: ubysciteSciTE.exe. If you opt to do the same, press F5 to run the hello.rb program or reach for the Tools menu.

Figure 3.2. Figure 3-2

In its extreme simplicity, this example already offers grounds for a few important considerations.

You run your program by simply invoking the Ruby interpreter (aptly named ruby). Being an interpreted language, you didn't have to compile it first, obtain an executable, and then run it.

From the first line you can see how Ruby comments begin with a # character and continue for the rest of the line. You can also place comments in line with code:

puts 'Hello, Ruby!' # Saying hi from Ruby

When commenting multiple lines you can either have a series of # characters or opt for embedded documents, which start with =begin at the beginning of a line, and end with =end at the beginning of another:

=begin This is an embedded document.
Anything in here is considered a comment.
No matter how many lines it spans.
=end

In this example, you are passing a string to the puts method. As you can see, in Ruby the parentheses around the arguments of a method are optional, even though they are highly recommended in all but the most trivial cases (puts is an example of a method for which it is usually considered okay to omit the parentheses). One clear exception is for methods that don't accept any parameters; in this case the parentheses should be omitted (for example, use my_method not my_method()).

NOTE

Ruby is a case-sensitive language. Don't capitalize method names.

Literals delimited by single quotes are instances of the class String. Double quotes can be used as well (for example, "Hello, Ruby!"), but in Ruby there are a couple of fundamental differences between the two. If a double-quoted string includes an escape sequence, for example, the newline (that is, ), this is correctly interpreted as a single character, which moves the cursor to the next line in the case of newline. With strings defined through single quotes, the newline is considered by Ruby to be a sequence of two regular characters (the backslash and the letter n, respectively). String literals defined with double quotes can also be used for string interpolation, a concept explained later on in this chapter.

NOTE

Unlike C# and Visual Basic, Ruby doesn't have a special data type reserved for single characters. Characters are just strings of length one (for example, 'a' or "a").

This example uses the method puts, which is the most commonly used by Rubyists for printing to the standard output. A couple of common alternatives are print and printf. The difference between puts and print is roughly equivalent to the difference between Console.WriteLine and Console.Write in .NET. But there is a twist. puts is smart enough to place the cursor on a new line after printing each of its arguments, but only when the argument needs it because a is missing at the end (print doesn't do this).

print "Hello, Ruby!
" # Equivalent to: puts 'Hello, Ruby!'

printf is a more advanced method that is used in order to apply a format string to a list of arguments:

printf("$%.2f", price) # e.g. prints $48.95

If you've ever programmed in C or C++, you should already be familiar with this method.

You may also notice that there is no need for semicolons at the end of the line, as is the case with Visual Basic as well. Semicolons can still be useful though, such as whenever you feel the need to place several statements on the same line:

puts 'Hello, Ruby!'; puts 'Hello again!' # Two statements on the same line

One final consideration it is important to make is the immediacy of the program. Yes, it's one of the most trivial programs you could possibly write, but it's already much more concise than its equivalents in compiled languages such as C# and VB.NET.

Visually compare them:


Ruby

puts 'Hello, Ruby!'


C#

using System;

class Hello
{
static void Main(String[] args)
    {
Console.WriteLine("Hello, Ruby!");
    }
}


VB.NET

Imports System

Class Hello

    Shared Sub Main()
Console.WriteLine("Hello, Ruby!")
    End Sub

End Class

There's certainly no need to create wrapping methods and classes just to print a string. And unlike the C# and VB.NET examples, the Ruby Hello world reads like English. The general idea is that Ruby tries to remove obstacles from the developer and strives not to get in the way.

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

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