RubyA Diamond of a Programming Language? : Page 2

Ruby is an object-oriented, meta-programming language that has many developers wondering if there are actually better alternatives to languages like Java and C#.

by Jim White

Mar 20, 2006

Page 2 of 8

Running Ruby
Like many interpreted languages, Ruby offers programmers a couple of ways to develop code. You can run Ruby in interactive mode using a command line tool or you can create a Ruby program file and ask Ruby’s interpreter to execute the program.

In Windows, bring up a Command Prompt window, simply type "ruby" at the prompt, and hit the Enter key (the Ruby \bin directory should be on your path for this to work). Ruby is now awaiting your program. Type in the program below, hit Control-D and then the Enter key. You should see Ruby execute your program as shown in Figure 1.

Figure 1. Running the Celsius to Fahrenheit Calculator in Ruby Interactively: Start Ruby and enter a program and then hit the end of file character (Control-D). Ruby displays the results and exits.

Figure 2. Running convertCtoF.rb: The Ruby interpreter can also execute a Ruby program file.

By the same token, the Celsius to Fahrenheit program above can be written with a Ruby IDE or simple text editor saved to a filefor example convertCtoF.rb (.rb is the conventional file type for Ruby programs). Now the Ruby interpreter will execute the Ruby program in the program file as shown in Figure 2.

The idea of having an interactive environment is not new, but it is quite powerful. Those familiar with Smalltalk, Common Lisp Object System (CLOS), or other interpreted programming environments of the past will find it very familiar. The interactive nature allows you to experiment with small chunks of programming code. A special Ruby batch file, irb.bat, kicks off the interactive Ruby interpreter and makes this point even more apparent. Figure 3 shows Ruby started using the irb.bat command. Now, code can be entered, interpreted, and tested one line at a time.

Figure 3. Interactive Ruby: In this example, the Ruby interpreter is started in interactive mode (using the irb.bat file provided with the Windows Installer) and several lines of Ruby code are entered and tested.

Figure 4. . fxri’s interactive Ruby Capability: The graphical language documentation guide, fxri, is also used here to run the same Ruby commands as in Figure 3, but from inside this documentation tool.

The interactive Ruby feature is built into several tools as well. For example, the graphical interface to the Ruby documentation, called fxri, not only serves as a language guide, but as an interactive Ruby interpreter as well (see Figure 4).

Objects, Methods, and Classes
In Ruby, everything is an object, and I do mean everything. Again, for those that have programmed in highly object-oriented languages such as Smalltalk, Eiffel, or CLOS, this will be a welcome surprise. In fact, one blog writer suggested that it is "time to learn Ruby or relearn Smalltalk" giving you a precursor to the nature and feel of Ruby. Numbers like 1, 2, 3 or 10.8 are all objectsnot primitive types as they are in Java or C++. Strings are objects. Classes and methods are objects. For example, the following is legal Ruby code (comment lines in Ruby are demarcated with "#"):

#absolute value of the object -34
-34.abs
#Round a float object
10.8.round
#return an uppercased, reversed copy of a string object
"This is Ruby".upcase.reverse
#return the number of arguments to the Math "sin" method.
Math.method(:sin).arity

Figure 5. Ruby Is All Objects: In Ruby, integers, floats, strings, and even classes and methods are all objects. The code here demonstrates method calls on these types of objects.

And in Ruby, work is done by calling on methods or operations of objects. What may look like a function or a procedure call in other programming languages is, in fact, a method call in Ruby.

As in all object-oriented programming languages, objects are created from classes. Many pre-built classes are provided with the Ruby library. You can modify these classes or build your own classes. Classes are defined with the "class" keyword. Class names start with an uppercase letter. Class definitions end with the "end" keyword. So a Rectangle class can be defined with the following code.

class Rectangle
end

To add methods to the class, use the keyword def. Methods also end with the end keyword. Method parameters follow the def keyword and method name. Adding an area method to the Rectangle class above would look like the following code.

class Rectangle
def area (hgt, wdth)
return hgt*wdth
end
end

For those familiar with other programming languages, you may have noticed a few differences. Ruby does not use any braces to delimit classes or methods, nor does it use semi-colons or other characters to denote the end of programming lines. The goal of Ruby, according to its creator, is to be simple, easy and "fun" to code. Who wants to remember all those semi-colons? Not fun. As long as you put statements on one line, no semi-colons or other code line ending mark is needed. By the way, the parentheses around the arguments to the area method are not required either. And by default, Ruby returns the last thing evaluated in a method, so the return keyword is not required. Therefore, you could have simply coded Rectangle as shown below:

class Rectangle
def area hgt, wdth
hgt*wdth
end
end

While legal, it is recommended that parentheses be used for method parameters for better legibility.