Classes and Methods

Ruby is a very object oriented language, with a rigid implementation of encapsulation, all is an object, common operators also are class methods and each action is done in the environment of an object; also in the interactive shell, or in the main program, one acts in the scope of an object.

The current object is referred to by the keyword self, which, in the main program and in the interactive usage, refers to an instance named main which acts as a default instance for method calls (it is a default receiver).

Ruby has only single inheritance, but classes can include modules (this is called "mixin"): a module defines a space for names and is a collections of methods, classes and constants, usually placed in an external file. When a module is mixed into a class, the methods defined in the module are inserted into the namespace of the class. This is a way to reuse function definitions and define common constants.

A Ruby class can have only one parent, but many modules can be mixed into a class.

All classes have, as a parent, the class "Object", which, since Ruby 1.9, inherits "BasicObject": a nearly empty class, with only some very basic functions implemented, as the equality operator "==".

The class Object includes (mixins) the Kernel module; this module contains a lot of methods. Are also implemented as methods of the Kernel module the loop statement, the exit method, the statements used to deal with exceptions, methods to open and read files and other functions that in many language are basic statements. The Kernel module implements also some equality operators.

Definition of classes are object too: instance of the class "Class" which inherits the class "Module" (which inherits "Object"); the class Module has some methods for access rules, mixins and reflection. When executing the statements in the class definition self is a reference to this instance of Class.

Instance and Class Variables

Variables defined into a class can belong to an instance or to the class; class variables are prefixed with "@@", and are unique to all the instances; can be defined in the class body or in a class method; instance variables are prefixed with "@" and each instance has its personal value of these variables. Also the class has a personal copy of the instance variables.

The instance variables must be defined and initialized into methods, if in the class body they are seen as instance variables of the class definition, seen as an instance of the class Class.

Both classes and instance variables are encapsulated into the class and can be seen from the outside only by means of special methods, named: accessors. Local variables are never seen outside their class or method, and class method don't see local variables defined outside the method itself; local variables are really local.

In spite of the rigid encapsulation instance variables can be obtained by the method: "instancename.instance_variable_get(:@name_of_var)" , and class variables with: "Classname.class_variable_get(:@name_of_var)". There are also the methods "class_variable_set" and "instance_variable_get", to set instance and class variables.

Methods and Functions

In Ruby all functions are methods; also if defined in the main program or in the interactive session, they belong to a class or an instance.

The class or instance whose method is called is the receiver for the method, the environment in which the method operates: the method sees all the variables of its receiver.

In the main program or in the interactive usage there is a default receiver instance named "main", which can be omitted in function calls; otherwise functions are referred to by prefixing their names with a class or instance name, separated from the function name by a dot.

To be called, every function must be associated to a receiver, which can be an instance or a class. There is a distinction between class methods and instance methods. Instance methods have an instance as the receiver, class methods have a class as the receiver, they are two different kind of methods.

Methods are not objects in Ruby, but there is a class, the class: Method whose instances store instance methods. These methods are still bound to their original instances, with access to instance methods and instance variables.

An instance of Method can be detached from its receiver, becoming an "UnboundMethod" object (which can't be called) and then re-associated to another instance. The function unbind detaches a method (as instance of Method) from its receiver; the function bind associated an UnboundMethod to a new instance.

The class Method, has the functions call to execute the method, and functions to return some method features, as: name, parameters, receiver (returns the instance), owner (returns the class), source_location (source filename).

Methods are associated to symbols with the same name.

In a Ruby file every function must be defined before its usage.

Public, Private and Protected Methods

In Ruby there are different access rules for methods: methods can be public, private or protected.

Private methods can be called only into their class, by other methods of the class.

Protected methods can be called in the class, in derived classes, or as instance attributes by object of the same class or a derived one.

By default methods are public, visible everywhere; methods can be made protected or private in the class definition, by putting them in the appropriate section of the class body; or with the statement: "protected" put after the class definition.

A derived class can redefine also the access properties of a method defined in a parent class.

The statements defining the access properties of a method: public, private and protected, are implemented as methods of the class: "Module".