Achieve quick solutions with this Ruby in the rough

The Ruby scripting language provides a straightforward methodology that's easy to learn. This introduction shows you how to use Ruby to quickly develop object-oriented applications and includes a sample script that demonstrates Ruby's syntax.

The Ruby-interpreted scripting language is an easy-to-learn environment for quickly developing object-oriented applications. Most frequently compared to Smalltalk, Python, Perl, C++, and Java, Ruby is a useful tool for rapid development and creation of prototypes. This article introduces Ruby and provides a simple script to illustrate its familiar and powerful syntax.

Ruby highlights
Ruby was created in 1995 by Japanese national Yukihiro Matsumoto as a way to easily process text and create system management tools. Since then, it has rapidly grown in popularity, thanks to its unique object-handling and inheritance structure.

In Ruby, everything is an object. For example, if you were to assign a variable with a value of 42, the number 42 itself is an instance of the integer object class, which is then stored as a value. Standard classes are user-extensible, giving the language ultimate flexibility. Additionally, C++ and Ruby classes can be inherited on the fly, enabling applications to make use of functions that Ruby itself doesn’t provide.

All variables are assigned and typed at run time. This eliminates the need for predefining variables, although mechanisms are available for specifically typing values if necessary. This also means that special syntax is not used to tell Ruby what type a variable is. For example, while @varname represents an array in Perl, typing is purely contextual in Ruby.

In contrast, syntax is used to control a variable’s scope. For example, variable names beginning with a lowercase letter are local variables; those beginning with a capital letter are constants; those beginning with $ are global; and those beginning with @ are instance variables. Other scope variables exist, along with several special global variables.

Ruby allows only single inheritance. This may sound limiting at first, but it means that naming clashes and other issues stemming from multiple inheritance are avoided altogether. Ruby does provide a mechanism for overcoming the perceived difficulties single inheritance may cause, called “mixing in,” where methods from other classes may be included into a subclass that is then inherited.

Other features that help define Ruby as a language include garbage collection, method overloading, and dynamic typing and definitions. Let's walk through the sample script in Listing A for a look at Ruby’s syntax.

Sample script
This script is run as an interactive session in a DOS or UNIX shell. I’ve outlined some of the more basic constructs of Ruby, but a wealth of user-defined loops and other interesting features is available.

First, we begin with a simple prompt. Notice that Ruby has no line termination syntax.
print "What is your favorite color?: "

Next, the script uses the predefined gets method to get a string from standard input. If an error occurs and the variable favcolor cannot be defined, the script exits.
favcolor = gets
exit if not favcolor

The chomp! function removes the trailing return character from the string. The bang (!) symbol signifies to the programmer that this is a destructive method, meaning it will modify the value of favcolor.

The case statement evaluates favcolor and performs an action that depends on its value. Notice the lack of parentheses and braces. The end of the loop is signified by the end keyword.
case favcolor
  when "red"
    print "You must like cherries!\n"
  when "blue"
    print "You must like the ocean!\n"
  when "green"
      print "Green is my favorite, too!\n"
    print "Your favorite color is ugly. Goodbye.\n"

Another type of loop, unless, evaluates the same as the statement, if favcolor != “green”.
unless favcolor == "green"

The \ symbol below continues the command on the next line. The #{ } syntax tells Ruby to evaluate the contents.
  print "I don't like #{ favcolor }, “ \
  “so I don't want to play anymore.\n"

If that statement is parsed, the script continues and discovers there is nothing left to do, so it performs garbage collection and exits at the end of the file. To explicitly exit here, you could use an exit statement. If the unless statement isn't satisfied, the script continues with the else clause.
  print "Will you be my best friend? (y/n): "

Again the script grabs user input, chomps it, and looks for an error.
  friend = gets
  exit if not friend

This while loop will prompt the user continuously until it gets a y or n value and matches the regular expression, /[yn]/. The prompt above could have been included here, guaranteeing that the loop would fail the first time through before getting user input, but I wanted to put in a different message.
  while friend !~ /[yn]/
    print "I said, will you be my best friend? (y/n): "
    friend = gets
    exit if not friend

When the script finally gets an acceptable answer, it executes this if statement.
  if friend == "n"
    print "You're mean. I'm leaving.\n"
    print "Great! I'll be over “ \
    “in a second - see you soon!\n"

This ends the unless loop we started above. At the end of the file, the script exits.

Where to get Ruby
Rare gem
In the forward to the book The Ruby Way, Matsumoto states that in developing Ruby, he “followed the Principle of Least Surprise” to create a scripting language that is “human-oriented” and that feels natural to developers. Indeed, Ruby provides a straightforward, logical methodology for quickly achieving the solution you seek.


Editor's Picks