Difference between revisions of "Simple Ruby Examples"

From Techotopia
Jump to: navigation, search
(New page: Not only is Ruby a flexible scripting language in terms of its syntax, it is also highly flexible in the ways in which scripts can be executed. In this chapter we will begin bu looking at ...)
(No difference)

Revision as of 20:35, 11 November 2007

Not only is Ruby a flexible scripting language in terms of its syntax, it is also highly flexible in the ways in which scripts can be executed. In this chapter we will begin bu looking at some simpl,e Ruby examples, and then look at the variety of different ways Ruby code can be executed.


Contents


The Most Basic Ruby Example

Programming guides tend to follow the tradition of using "Hello World" as the first example of using a programming language. Typically such a program does nothing but output the string "Hello World" to the console window. Ruby Essentials is no exception to this rule, though we will modify it slightly to display "Hello Ruby!". Without further delay, let's get started:

print "Hello Ruby!\n"

As you can see, all we need to do to output some text is enter a single line of Ruby code. You may recall, if you have read the previous chapters, that one of the strengths of Ruby is that it is fast and intuitive to learn. Clearly it would be hard to make printing a line of text any easier than a print statement followed by the text to be displayed (together with a newline character '\n').

As an example, let's compare this to the equivalent code in Java:

import java.io.*;

public class Hello {

        public static void main ( String[] args)
        {
                System.out.println ("Hello Ruby!\n");
        }
}

Hopefully, you are now beginning to see why Ruby is so popular. Before we go any further we need to learn how to execute our first Ruby example. This can be done in a number of ways, each of which shall be covered in the following sections of this chapter.

Executing Ruby from the Command Line

Ruby allows lines of code to be executed as command line options to the ruby tool. This is achieved by using the '-e' command line flag. To execute our example 'Hello Ruby!' code, therefore, we could enter the following command:

ruby -e 'print "Hello Ruby!\n"'
Hello Ruby!

The '-e' flag only allows a single line of code to be executed, but that does not mean that multiple '-e' flags cannot be placed on a single command line to execute multiple lines:

ruby -e 'print "Hello Ruby!\n"' -e 'print "Goodbye Ruby!\n"'
Hello Ruby!
Goodbye Ruby!

Executing Ruby from a File

Clearly the command line approach to execution is of limited use once you get beyond a few lines of Ruby script. A much more common approach is to place the Ruby script in a file, and then pass that file to the Ruby interpreter to run. To try this, create a file called ruby.rb using your favorite editor and enter the following lines into it:

print "Hello Ruby!\n"
print "Goodbye Ruby!\n"

To execute this script, simply refer to it on the command line when launching ruby:

ruby hello.rb
Hello Ruby!
Goodbye Ruby!

Creating a Self Contained Ruby Executable on Linux or UNIX

Placing Ruby code into a file brings is obviously much easier and practical than using multiple -e command line options. Suppose, however, that we want to go one step further and be able to execute a Ruby based program simply by typing the name of the file containing the code, rather than prefixing it with the ruby command.

This can be achieved on Linux or UNIX by placing a special line at the top of the script file informing the environment responsible for executing the program (such as a Linux command shell) where to look for the Ruby interpreter. This special line consists of a '#', a '!' and the path the ruby command and is known affectionately as the she-bang.

Firstly, you need to know where ruby is located on your system. Assuming it is already in your PATH environment variable you can use the which command to find it:

which ruby
/usr/bin/ruby

Given that ruby is in /usr/bin on the above system we can modify our sample application accordingly:


#!/usr/bin/ruby
print "Hello Ruby!\n"
print "Goodbye Ruby!\n"

We can now try running our hello.rb script:

./hello.rb
-bash: ./hello.rb: Permission denied

Clearly if you got the above output, there is a problem. This is simply a matter of the script not having execute permission. This can easily be rectified by using the chmod command to add execute permission to our script:

chmod 755 hello.rb

If we now try again we should get better results:

./hello.rb
Hello Ruby!
Goodbye Ruby!

Associating Ruby Files on Windows

Interactive Ruby Executions