O'Reilly logo

Ruby in a Nutshell by Yukihiro Matsumoto

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 1. Introduction

Ruby has been readily adopted by programmers in Japan and has had much documentation written for it in Japanese. As programmers outside of Japan learn about the benefits of Ruby, there is a growing need for documentation in English. The first book I wrote for O'Reilly, Ruby Pocket Reference, was in Japanese. Since then Ruby has changed significantly. To meet the needs of non-Japanese programmers, we translated, updated, and expanded Ruby Pocket Reference into Ruby in a Nutshell.

Ruby is an object-oriented programming language that makes programming both enjoyable and fast. With the easy-to-use interpreter, familiar syntax, complete object-oriented functionality, and powerful class libraries, Ruby has become a language that can be applied to a broad range of fields from text processing and CGI scripts to professional, large-scale programs.

While Ruby is easy to learn, there are many details that you can't be expected to remember. This book presents those details in a clean and concise format. It is a reference to keep next to your desktop or laptop, designed to make Ruby even easier to use.

For those of you who are new to Ruby, there are several online tutorials available to get you started: Ruby's home page (http://www.ruby-lang.org) is a good starting pointing as it offers Ruby tutorials and the Ruby Language FAQ.

Ruby's Elegance

Ruby is a genuine object-oriented scripting language designed from the ground up to support the OOP model.

Most modern languages incorporate aspects of object-oriented programming. Because Ruby was designed from the beginning to support OOP, most programmers feel it is elegant, easy to use, and a pleasure to program. Everything in Ruby is an object; there's no exception.

While Ruby is object-oriented, you can also use Ruby to do procedural programming. But as you do, Ruby is secretly turning your nifty procedures into methods on a globally accessible object.

Throughout the development of the Ruby language, I've focused my energies on making programming faster and easier. To do so, I developed what I call the principle of least surprise. All features in Ruby, including object-oriented features, are designed to work as ordinary programmers (e.g., me) expect them to work. Here are some of those features:

Interpretive programming

No compilation is needed; you can edit and feed your program to the interpreter. The faster development cycle helps you enjoy the programming process.

Dynamic programming

Almost everything in Ruby is done at runtime. Types of variables and expressions are determined at runtime as are class and method definitions. You can even generate programs within programs and execute them.

Familiar syntax

If you've been programming in Java, Perl, Python, C/C++, or even Smalltalk, Ruby's syntax is easy to learn. The following simple factorial function illustrates how easily you can decipher its meaning:

def factorial(n)
  if n == 0
    return 1
  else
    return n * factorial(n-1)
  end
end
Iterators

The iterator feature for loop abstraction is built into the language, which means a block of code can be attached to a method call. The method can call back the block from within its execution. For example, Array has the each method to iterate over its contents. With this feature, you don't need to worry about the loop counter or boundary condition.

ary = [1,2,3,4,5]
ary.each do |i|
  puts 1*2
end  # prints 2,3,4,8,10 for each line

A block is used not only for loops. It can be used for various purposes including the select method of Array, which uses blocks to choose values that satisfy conditions from contents:

ary = [1,2,3,4,5]
ary = ary.select do |i|
  i %2 == 0
end  # returns array of even numbers.
Exceptions

Just as you'd expect in a modern OOP language, Ruby provides language-level support for exception handling. For example, an attempt to open a file that doesn't exist raises an exception, so that your program doesn't run, assuming an unmet precondition. This feature obviously enhances the reliability of your programs. Exceptions can be caught explicitly using the rescue clause of the begin statement:

begin
  f = open(path) 
rescue
  puts "#{path} does not exist."
  exit 1
end
Class libraries

Ruby comes with a strong set of bundled class libraries that cover a variety of domains, from basic datatypes (strings, arrays, and hashes) to networking and thread programming. The following program retrieves the current time string from the local host via a network socket connection:

require "socket"
print TCPSocket.open("localhost","daytime").gets

In addition to bundled libraries, if you go to http://www.ruby-lang.org/en/raa.html shows a list of the many unbundled useful libraries along with applications and documentation. Since Ruby is rather young, the number of libraries available is smaller than that of Perl, for example, but new libraries are becoming available each day.

Portable

Ruby ports to many platforms, including Unix, DOS, Windows, OS/2, etc. Ruby programs run on many platforms without modification.

Garbage collection

Object-oriented programming tends to allocate many objects during execution. Ruby's garbage collector recycles unused object automatically.

Built-in security check

Ruby's taint model provides safety when handling untrusted data or programs.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required