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.

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.