The Object Class

The class "Object" is the main class in the Ruby language. The "Kernel" module, containing most of the basic functions, is mixed into the Object class, and the Object class is inherited by all the Ruby classes, making all its methods available to every instance of every class.

Among the methods in the Object class, some are used to find the class of an object, to test methods of an object , to interact with the operating system. Some of these methods are listed in the following table.

Method Function Examples
nil? test if nil (only a nil value returns true) Es,: nil.nil? => true
is_a? ; kind_of? tests if class or ancestor 1.is_a? Numeric => true
instance_of? tests if instance of a class 1.instance_of? Fixnum => true
respond_to? test if a class method 1.respond_to?('+') => true
methods array with public methods Object.methods => [:allocate, :new,....]
method returns a Method object 1.method("+") => #<Method: Fixnum#+>
send calls a method on an instance 1.send("+",2) => 3
clone clones an object s2=s1.clone
dup makes a shallow copy of an object (copies only the references, not the data)
abort stops the program abort("messaggio")
exit exits from the program, raising SystemExit. Es.: exit(1)
exit! exits, skipping then exception handling mechanism
fork creates a sub-process
exec executes a process, in place of the current one. exec("ls")
spawn executes a command in a subshell. spawn("ls")
sleep(s) pause the program for some seconds sleep(5)
system executes a system command. system("ls")
command command for the system ls => lists current directory
%x{command} command for the system as ``, but using a different separator
warn writes a message on stderr. warn("attention")
eval evaluates a ruby expression. a=1;b=2; eval("a+b") =>3
load loads and execute a ruby program. load("nomefile.rb)
freeze makes an object immutable
frozen? tests if an object is immutable

The class Object has also methods to build basic objects: Integer, Float, Complex, Rational, Array and String.

The operator: "===" is used to test if an object is an instance of a class or an instance of its descendants, the descendants redefine the operator to allow for comparison. This operator is mainly used in the "case" statement.

In Ruby there aren't immutable objects, as in Python. All objects can be changed, but there is a method: freeze, which prevents every change in a single object, making that object immutable. A freezed object can't be unfrozen.

Logical Classes

There is no boolean class in Ruby, but two special classes: TrueClass, FalseClass. These classes have an unique instance: true and false representing the results of logical expressions.

There is also: "nil", the unique instance of NilClass, meaning a missing or undefined value; nil evaluates to false, but all valid numbers, zero included, evaluated to true, and also void strings, void arrays or hashes evaluates to true.

"nil? (a method of Object) return true if an object evaluates to nil,

The operator: "defined?" returns nil if an object is not defined; there are no true? or false? operators, it's easy understand why: in Ruby everything is true.

TrueClass, FalseClass and NilClass implement the logical operators: "& | ^". The NilClass implements also some conversion operators, to change nil objects into void objects (but void objects don't evaluate to nil):

nil.to_a  => []       : empty array
nil.to_c  => (0+0i)   : complex zero
nil_to_s  => ""       : empty string
nil.to_f  => 0.0      : zero
nil.to_i  => 0
nil.to_r  => (0/1)    : rational zero