oreilly.comSafari Books Online.Conferences.


An Introduction to Ruby
Pages: 1, 2

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
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

# 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:

  • Locals start with lowercase.
  • Instance variables are prefixed with '@'.
  • Class variables start with '@@'.
  • Constants start with an uppercase letter.
  • And finally, globals start with a dollar sign, '$'.

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:


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:

  • Numerous libraries, extensions, and modules are available, and new ones are showing up almost daily.

  • The Ruby community's focal point, the Ruby mailing list, is abuzz with activity.

  • In November, O'Reilly will publish Ruby in a Nutshell written by Matz himself.

  • The first annual Ruby Conference was held October 12-13, 2001 in Florida.

  • And you can always find great pointers on the Ruby Home Page itself, too.

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.

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: