Ruby Summary

  1. Ruby gives you three levels of protection:
    • public - methods can be called by everyone. A class's instance methods are public by default.
    • protected - methods can be invoked only by objects of the defining class and its subclasses.
    • private - methods cannot be called with an explicit receiver - the receiver is always self. This means that private methods can be called only in the context of the current object. The initialize method is always private.
  2. Access control is determined dynamically, as the program runs, not statically. You will get an access violation only when the code attempts to execute the restricted method.
  3. Top-level methods are private instance methods of the Object class.
  4. attr_reader is reader only; attr_writer is writer only and attr_accessor is both reader and writer.
  5. An exception is a special kind of object, an instance of the class Exception or a descendant of that class.
  6. The raise method is from the Kernel module. By default, raise creates an exception of the RuntimeError class. To raise an exception of a specific class, you can pass in the class name as an argument to raise.
  7. To do exception handling, we enclose the code that could raise an exception in a begin-end block and use one or more rescue clauses to tell Ruby the types of exceptions we want to handle.
  8. It is to be noted that the body of a method definition is an implicit begin-end block; the begin is omitted, and the entire body of the method is subject to exception handling, ending with the end of the method.
  9. If you write a rescue clause with no parameter list, the parameter defaults to StandardError.
  10. If you need the guarantee that some processing is done at the end of a block of code, regardless of whether an exception was raised then the ensure clause can be used. ensure goes after the last rescue clause and contains a chunk of code that will always be executed as the block terminates. The ensure block will always run.
  11. By default, the inspect message, which can be sent to any object, formats the object's ID and instance variables. It returns a string containing a human-readable representation of object. If not overridden, uses the to_s method to generate the string.
  12. When writing rescue clauses, ensure that you rescue from subclassed Exceptions first and then more general Exceptions, otherwise you will not rescue the more specific error. For example, you would rescue based on NoMethodError before NameError, and those before the more general (superclass) StandardError.
  13. The Time class contains Ruby's interface to the set of time libraries written in C.
  14. Time zero for Ruby is the first second GMT of January 1, 1970.
  15. Ruby's DateTime class is superior to Time for astronomical and historical applications, but you can use Time for most everyday programs.

Note: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various Ruby Gurus), my acknowledgment and thanks to all of them. Much of the material on and in the course at is drawn primarily from the Programming Ruby book, available from The Pragmatic Bookshelf.