Home > Articles > Programming > Ruby

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Brief Summary of Ruby Language

Methods

Every procedure in Ruby is a method of some object. Some method calls appear to be function calls as in other languages, but in fact they are actually invocations of methods belonging to self. Parentheses can be omitted if unambiguous.

  "string".size()              # call method of "string"
  "string".size                # parentheses can be omitted.

  print "hello world\n"        # call `print' of self

As in Smalltalk, in Ruby operators are special forms of method calls. Consider this, for example:

  1 + 1	# invoke + method with argument 1
  ary[0]	# i.e. `ary.[](0)'

  ary[0] = 55 # i.e. `ary.[]=(0,55)'

Unlike in Smalltalk, operators follow usual operator precedence of programming languages, thus:

  1 + 2 * 3 == 1 + (2 * 3)

To define methods, use def statements.

  def foo(arg1, arg2)
    print arg1, arg2, "\n"
  end

A def statement at the top level defines a global method that can be used just like a function. Usual methods can be defined by def statements within class definition statements.

  class Foo
    def foo
      print "foo method in Foo class\n"
    end
  end

Blocks

Blocks in Ruby are something in between CLU's iterators and Smalltalk's blocks. Like CLU's iterator, a block is a special syntax to pass extra procedural information to the method. For example:

  10.times do |i|
    print i, "\n"
  end

Here, 10.times is a method invocation, with a block starting from "do" to "end." The times method of Integer class evaluates a block 10 times with incremental values from zero. Values given to the block are assigned to the variable surrounded by || for each iteration.

In this code, do .. end can be replaced by ....

As in Smalltalk, blocks are used not only for loop abstraction, but also for conditionals, callbacks, and so on:

  [1,2,3].collect{|x| x*2}     # => [2,4,6]
  [1,2,3].delete_if{|x| x == 1}       # => [2,3]
  [1,2,3].sort{|a,b| b<=>a}    # => [3,2,1]
  "abc".sub(/[ac]/){|x|x.upcase}# => "AbC"

Blocks in Ruby are not first-class objects; rather, you can objectify them explicitly. Objectified blocks are sometimes are called closures because they wrap code blocks along with local variable bindings:

  c = lambda{|x| x + 2}               # objectify block, returns closure
  c.call(5)                    # => 7, evaluate closure

The word lambda is traditionally used in the Lisp world for the function to make closure. Ruby provides the plain alias proc, too.

No Declarations

A variable in Ruby programs can be distinguished by the prefix of its name. Unlike in Perl, the prefix denotes the scope of the variable. It removes declarations from Ruby to let you code less. In addition, by knowing variable scope at a glance enhances readability.

Scope

Prefix

Example

global variable

$

$global

instance variable

@

@instance

local variable

lowercase or _ local

 

constant

uppercase

Constant

class variable

@@

@@class_var (since 1.5.3)

Local variable scope can be introduced by the following statement: class, module, def, and blocks. Scopes introduced by class, module, and def do not nest. But from blocks, local variables in the outer scope can be accessed. Local variables in Ruby follow lexical scoping.

Literals

Ruby provides a variety of literal notations for maximum flexibility—especially for strings. For example:

  "string with escape\n"
  'without escape\n'
  `echo command output pseudo string`
  %q!perl style single quote!
  %Q!perl style double quote\n!
  %w(word list)
  /regular expression/
  %r!another regex form!
 
  <
    

Literals in Ruby are very similar to those in Perl, except that some require % at the front.

Inheritance and Mix-in

Object-oriented features in Ruby were also carefully designed. Ruby supports single inheritance only, which I consider to be a feature. With single inheritance, class hierarchy forms a comprehensive tree, unlike complex network with multiple inheritance. Instead of multiple inheritance, Ruby supports mix-in, the capability to add a set of attributes (methods, constants, and so on) to a class. This set of attributes is called a module. A module is a special kind of class that does not have an explicit superclass, that cannot be instantiated, and that can be mixed into other classes or modules.

An arbitrary class hierarchy formed by multiple inheritance can be reformed using single inheritance and mix-in:

Figure 1: Multiple Inheritance

Figure 2: Single Inheritance

The mix-in class hierarchy is defined as follows in Ruby:

  class Stream
    # defines basic Stream features...
  end
 
  module Readable
    # defines Readable Stream features...
  end
 
  module Writable
    # defines Writable Stream features...
  end
 
  class ReadStream
    

You may feel that this is more restrictive than multiple inheritance, but I believe that you will see how comprehensive the mix-in hierarchy is after you understand it. This method simplifies the class hierarchy extensively.

Individual Methods and Prototype-Based Programming

Although Ruby is classified as a class-based object-oriented language, it allows you to define methods for individual objects.

  foo = Object.new     # create an Object
  def foo.method1      # add a method to it
    print "method1\n"
  end
  foo.method1          # prints "method1\n"

In addition, it allows prototype-based object-oriented programming:

  bar = foo.clone      # make new object after prototype
  bar.method1          # `clone' copies individual method too
  def bar.method2      # new method added
    print "method2\n"
  end
  bar.method2          # prints "method2\n"

Although prototype-based programming is just a theoretical possibility in Ruby, individual methods can be used, such as for an event handler. For example:

  require "gtk"
  window = Gtk::Window::new(Gtk::WINDOW_TOPLEVEL)
  button = Gtk::Button::new("hello")
  window.add(button)
  def button.clicked   # event handler by individual method
    print "hello\n"
    exit
  end
  button.show
  window.show
 
  Gtk::main()

Exceptions

As a modern language, Ruby supports exceptions. All methods in the class library raise exceptions for unusual status. Ruby allows the programmer to omit most error detecting code, and it frees the programmer from worrying about execution under unsatisfied preconditions.

Exceptions are raised by the raise method:

  raise                   # raise unnamed RuntimeError exception
  raise "error!"          # raise RuntimeError with message "error!"
  raise IOError, "closed" # raise IOError with message "closed"

Exceptions are caught by the rescue clause of the begin statement.

  begin
    codeA
  rescue SomeError, AnotherError
    codeB
  end

If codeA raises an exception, which is a subclass of either SomeError or AnotherError, the exception is caught and codeB is executed. All exceptions are subclasses of the Exception class.

Exceptions require you to code—and worry—less less. This is more evidence that Ruby follows the principle of conciseness.

Threads

Ruby supports user-level threading. It's implemented by using setjump()/longjmp() and stack copying. It is not efficient and cannot utilize multiple CPUs. It is also several percents slower than sequential execution. However, it works on all platforms, including MS-DOS machines. Because Ruby behaves uniformly on all platforms, programmers need not worry about threading compatibility. Threading often improves programs' response time by making heavy procedures run in the background.

To start a new thread, you simply call Thread::new with a block:

  Thread::new do
    # code to executed in a thread
  end

To synchronize between threads, the standard class library provides classes such as Mutex and Queue.

Garbage Collection

The reference counting technique used by Python does not recycle circular references, so programmers in this language must cut circular references explicitly later. Ruby uses a conservative mark-and-sweep method of garbage collection, which frees programmers from worrying about circular references. The references are reclaimed automatically by the collector.

The garbage collector scans C stack and registers as well, so that C/C++ written extensions do not have to maintain refcounts or protect local variables. This makes extension development easier than it is for other languages. There is no INCREF/DECREF and no mortal.

The Class Library

Ruby's class library makes this language usable and strong.

This next figure is the digest from Ruby's built-in class library.

Figure 3: The Ruby Built-in Class Library

The inheritance hierarchy is shallow compared to that of Smalltalk. This is partly because implementation sharing is done by mix-in.

Basic data structure classes, such as Array, Hash, IO, String, and Regexp, are designed after Perl. That is, they are designed by reorganizing Perl functions cleanly and consistently into classes. Time, Range, and Numeric classes go beyond simple reorganization. For example, Ruby supports arbitrarily sized integer arithmetic.

Aside from the built-in class library, many add-on libraries exist. They are still small in number compared to CPAN, but they are growing rapidly. Here's the digest from the list of add-on libraries:

  • Matrix

  • Rational Number

  • Design Patterns (Singleton, Proxy)

  • Unicode manipulation

  • CGI

  • Socket

  • HTTP

  • POP3

  • FTP

  • SMTP

  • GUI (Tk, Gtk, and so on)

  • Database (PostgreSQL, Interbase, Oracle, MySQL, and so on)

The list goes on.

Performance

Typical Ruby programs tend to be smaller and faster than their Python equivalents, and almost the same size and a little bit slower than their Perl equivalents. Every operation in Ruby—even integer addition—is done by invoking methods, so the cost to search and invoke methods cannot be ignored, especially for simple programs. When programs get bigger and more complex, however, no significant performance difference should arise as a result of methods.

The following is the outcome of a simple longest-word search program over /usr/share/dict/words (409067 bytes). These were tested on my Pentium-200MHz Linux machine.

Program

Lines

Seconds

Ruby

14

1.046

Perl

15

0.593

Python

16

5.001

As stated before, Ruby is a bit slower than Perl because of the overhead for method searching; however, it's much faster than Python.

Benchmark programs:

Ruby:

  ---
  #!/usr/bin/ruby
 
  len = 0
  words = []
  while line = gets()
    if line.size == len
      words.push line
    elsif line.size > len
      words = [line]
      len = line.size
    end
  end
  for word in words
    print word
  end
  ---
 

Perl:

  ---
  #!/usr/bin/perl
 
  $len = 0;
  @words = ();
 
  while (<>) {
      if (length($_) == $len) {
          push(@words, $_);
      }
      elsif (length($_) > $len) {
          @words = ($_);
          $len = length($_);
      }
  }
  for $word (@words) {
      print $word;
  }
  ---
 

Python:

  ---
  #!/usr/bin/python
 
  import sys
 
  size = 0
  words = []
 
  for f in sys.argv[1:]:
    io = open(f)
    while 1:
      line = io.readline()
      if line == "": break
      if len(line) == size:
        words.append(line)
      elif len(line) > size:
        words = [line]
        size = len(line)
  for word in words:
    print word,
  ---
  • + Share This
  • 🔖 Save To Your Account