Ruby is a consistent object oriented language. There are no primitive data types. There are just objects. Even classes are objects. They also are called first-class objects. Each inherited class is at all times an object instance.
The Ruby class model
Objects are class instances. And the class itself is an instance of its parent class.
This can be proved easily. An example:
class Product
end
The class Product is a blank class, which is inherited from Class automatically:
Product.class # => Class
But its super class:
Product.superclass # => Object
is Object indeed. This means each class also inherits all the methods of Object.
So each product instance has specific instance methods, such as:
product = Product.new
product.dup # => #<Product:0x00000004b9e980>
product.send(:dup) # => #<Product:0x00000004b82690>
product.is_a? Object # => true
And because each class is also an instance of its parent class, the same instance methods can be applied to the class itself:
Product.dup # => #<Class:0x00000004b70170>
Product.send(:dup) # => #<Class:0x00000004b53a48>
Product.is_a? Object # => true
Each object has an object_id:
Product.new.object_id # => 36039100
So does each class have an object_id likewise:
Product.object_id # => 36039160
Class instance variables
Another important object feature is instance variables. They are identified by a single @.
Classes can also have instance variables (with a single @). Hence they only are valid in the class context, they are called class instance variables. There is a clear contrast to class variables (with @@). That difference plays an important role during inheritance.
An example:
class Product
@@vat_factor = 0.19
@warranty_months = 6
end
A product not only has a class variable @@vat_factor, but also a class instance variable @warranty_months.
First of all the class variables:
Product.class_variables # => [:@@vat_factor]
Then the class instance variables:
Product.instance_variables # => [:@warranty_months]
A product instance object has no instance variables:
Product.new.instance_variables # => []
and it also has no direct access to the class instance variables:
Product.new.instance_variable_get :@warranty_months # => nil
Product.instance_variable_get :@warranty_months # => 6