Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


An Introduction to Ruby

by Colin Steele
10/25/2001

Ruby is an object-oriented scripting language that combines the object-oriented programming (OOP) power of Smalltalk with the utility of Perl and the power of Python.

I can already hear you grumbling, "Oh, great, another language." Why should you care? You're already up to your elbows in technologies to learn, right? Well, call your significant other and tell him or her you're going to be late tonight. And go get another Jolt. You're going to be hooked on Ruby for the simple reason that Ruby makes programming fun again (and that's what really counts).

This article introduces Ruby by examining the high-level properties of the language as well as some important features that distinguish it from other languages. We'll compare Ruby with Perl and Python. Along the way, I'll provide my personal take on Ruby, and why I think you'll love it as much as I do.

Traits That Recommend Ruby

Looking down on Ruby from 50,000 feet, we see it has several qualities that recommend it as the next language you should learn, including its consistency and elegance, OOP features, ease of use, extensibility, and its applicability to rapid development.

But what exactly is Ruby? It's a weakly typed, interpreted, cross-platform language implemented in C, written from the ground up to be object-oriented. Ruby's been around since 1995, and has its largest following in Japan, the home of Ruby's author, Yukihiro Matsumoto, a.k.a. "Matz." Although Ruby is more popular in Japan than Python, you may not have heard much about it, yet. Or, if you have, you may have heard that it's a "better Perl than Perl."


O'Reilly & Associates will release Ruby in a Nutshell in November 2001. Beta Sample Chapter 4, Standard Library Reference, is free online.


I think there's a large measure of truth in that statement. Ruby somehow manages to combine some of the best features of Smalltalk, C, Perl, and Python in a package so elegant and natural that I found myself hooked after just one day.

Some of Ruby's strongest traits are:

These aspects of Ruby arise from the design of the language, its purpose, and its heritage. But aside from these more abstract positive qualities, there are several specific language features that you'll want to explore in Ruby. These include code blocks/closures, iterators, variable scoping by name, and its uniform object treatment.

Sample Code and Output

Related Reference

Ruby in a NutshellRuby in a Nutshell
By Yukihiro Matsumoto With translated text by David L. Reynolds Jr.
Table of Contents
Index
Sample Excerpt
Full Description

In Ruby, a block is a group of statements appearing next to a method invocation; it is the last argument passed to the method. Sometimes this block is referred to as an "anonymous inline callback." This code block isn't called in the lexical scope in which it appears; rather, it can be called from the method that is being invoked (using 'yield'). It's a form of callback, but has greater power because it can be used as a closure. A closure is code that "closes off" or "packages up" references to variables outside the lexical scope that exists at the time the code is executed. In simpler terms, Ruby's blocks allow you to simply and expressively pass behavior as well as data in method invocations. A method invocation becomes a dialog between the caller and the callee.


def method_that_calls_a_block(arg)
  
  puts "The argument to the method call was #{arg}."
  
  # Here, the variable 'xyzzy' (which is used in the 
  # block below) is undefined; it's not in this lexical 
  # scope.  But using a block, the code in the block 
  # can reference 'xyzzy'.

  # Now we call the block using 'yield', passing it 
  # the square of the argument to this method.
  
  yield arg * arg
  
end

# Our magic variable
xyzzy = 42


# Now we invoke method_that_calls_a_block, passing it 
# 2 as an argument.  The block is invoked from inside
# the method (above, using 'yield').

method_that_calls_a_block(2) { |arg|
  puts "Hello from the block.  Argument to the block is #{arg}."
  puts "xyzzy is #{xyzzy}."
  xyzzy += arg
  puts "Hello again.  Now xyzzy is #{xyzzy}."
}
puts "All done with the method.  Now xyzzy is #{xyzzy}."

The output from this contrived little demonstration is:

The argument to the method call was 2.
Hello from the block.  Argument to the block is 4.
xyzzy is 42.
Hello again.  Now xyzzy is 46.
All done with the method.  Now xyzzy is 46.

One of the most powerful, pervasive, and downright fun results of having blocks is Ruby's iterators. One of the nice things you'll find in most OOPLs is a set of classes for maintaining collections of objects. Yes, container classes certainly reduce drudgery, but there can be problems with them. Especially with designs that are a bit too "intimate." In Ruby, an iterator is a method implemented by a container using blocks as callbacks, which allows the iterator to supply the knowledge about how to traverse the collection and the invoker to supply the knowledge about how to perform operations on elements in the collection. This simple abstraction allows an elegant decoupling of collections and their contents. For example, to sort an array of strings by length (instead of by alphabetical order):

goofyStuff = ["orange", "phase-plasma cannon", "epistle"]
p goofyStuff.sort # Default compare - alphabetical
p goofyStuff.sort { |a,b| a.length <=> b.length } # We specify compare

The results are:

["epistle", "orange", "phase-plasma cannon"]
["orange", "epistle", "phase-plasma cannon"]

In Ruby, variables are scoped by name:

There is a certain resemblance to the "line noise" found in Perl, but in practice, the result is far more readable and intuitive. There's no variable declaration in Ruby, but with a glance, this scope-by-name feature allows you to tell what kind of variable you're looking at:

local_var
@instance_var
@@class_var
CONSTANT_VAR
$global_var

This lets you go a little crazy:

$xyzzy = @@xyzzy = @xyzzy = xyzzy = XYZZY

Lastly, in Ruby, everything is an object. The practical and positive results of this simple rule can't be overemphasized. There's a pure, intuitive joy to be found in calling methods on literals:

length = "Hello, world!".length
10.downto(1) { |n| puts n, "You're getting sleepier... " }

I can only look back and rue the days I spent writing Java code to flip-flop back and forth between the fundamental data type and the class for integers. Just say no, with Ruby.

Regarding Perl and Python

If you've gotten this far, Ruby probably sounds intriguing to you. But there's still that nagging question of "Is it better enough than Perl or Python to make it worth my while to learn it?" It's a valid question, and naturally, only you can answer it. But there are a few salient differences to consider.

Ruby owes much to Perl. It resembles Perl in many ways, sharing many shortcut globals (things like $0, $$, and so on), and it shares Perl's power to manipulate textual data. But Ruby is like a sleek, elegant Perl, with a fully integrated object model. It handles complex data structures better, and seems better suited to programming in the large. And, it's a heck of a lot easier to read.

My Perl background is quite a bit stronger than my experience with Python, and I found myself breathing an "Ahhhh" of relief with Ruby. There are some things that really bugged me about Perl. For instance, I have to agree with the sentiment that Perl seems to encourage a write-only coding style. It's just plain hard on the eyes. And there are some things that just make me shudder, like the lack of method access controls, and the lack of data inheritance. But, these things seem to melt away with Ruby.

Ruby has similar goals to Python, but a significantly different approach to meeting those goals. Ruby differs in the unified nature of its object model; Python uses a procedural-and-object hybrid approach that at times feels a bit contrived. I find all those 'self' references tedious. In Python, methods are objects, which they aren't in Ruby. Python supports multiple inheritance, which Ruby does not. On the other hand, Ruby also has much greater syntactic flexibility than Python. And Ruby makes superior use of blocks, closures, and iterators.

I think the biggest difference between Python and Ruby is philosophy. I suspect that folks will be drawn to one language or the other based on factors that are ineffable. Like me for example: I just don't like someone telling me how to indent my code. :)

For More Information

Ruby has a fairly small following, especially in the United States. But it's gathering up a head of steam:

Now's the time, kids. You can be the first hacker in your cubicle farm to start using Ruby. And you'll be glad you did, because Ruby makes programming fun.


O'Reilly & Associates will release Ruby in a Nutshell in November 2001.

Beta Sample Chapter 4, Standard Library Reference, is free online.


Colin Steele is an ex-AOL dilettante hoping to get his shamanic spirits to start coding for him.


Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.