Understanding Objects and Classes in Ruby

Introduction to Objects in Ruby

Objects in ruby

In the world of programming, objects are the building blocks of Object-Oriented Programming (OOP). They represent real-world entities and resources in a digital paradigm. In Ruby, objects play a fundamental role, and Ruby is often described as a true object-oriented language because everything in Ruby is treated as an object.

What is an Object in Ruby?

An object in Ruby can be understood as a collection of instance variables and a link to a class. Here are some examples that illustrate objects in Ruby:

# Numbers are objects
num = 1
num.object_id

# Strings are objects
chr = 'a'
chr.object_id

# Classes are also objects
class MyClass; end
MyClass.object_id

# Even methods are objects
method = num.method(:times)
method.object_id

# Operators are also objects (methods)
plus = num.method(:+)
plus.object_id

In Ruby, objects are first-class citizens, and various language constructs like classes, instance variables, methods, and modules are also objects. These objects coexist in a system known as the Object Model, where you can find answers to questions like “Which class does this method belong to?” or “What happens when I inherit from this class?” and “What instance variables does this object have?”

The Object Model in Ruby

The Object Model is a system where all these language constructs live together harmoniously. It provides insights into the relationships between classes, inheritance, method origins, and instance variables.


class A; end
class B < A; end

b = B.new
b.class
b.ancestors
b.superclass
# Modules do not fall into the superclass chain

# Which class does this method come from?
class A
  def new_method
    print "new method"
  end
end

class B < A; end

b = B.new
b.new_method
# b.method :new_method will show you the method's origin

# What instance variables does this object have?
b.instance_variables
# This will show an empty array if no instance variables are defined

Introduction to Classes

In Ruby, object methods don’t live within the objects themselves; instead, they reside in the object’s class, where they are referred to as instance methods of the class.

What’s a Class in Ruby?

A class in Ruby can be thought of as an object, specifically an instance of the Class class. It contains a list of instance methods and a link to a superclass. Interestingly, a class is also a subclass of Module, making it a module as well.

Like any other object, a class has its own methods, such as new(), and others you define. These methods are referred to as instance methods of the Class class and can be defined using the self. prefix.

class A
  # This method is local to this particular class
  # Other descendants of the Class class don't have it
  # This is a singleton method
  def self.some_method
    print "some method"
  end
end

A.singleton_methods
# [:some_method]

A.some_method
a = A.new
a.some_method # This method is not defined for instances

In Ruby, classes must be accessed through references. Each class has a constant reference, which is the class’s name itself.

Summary

Ruby’s object-oriented nature revolves around objects and classes. Objects are collections of instance variables and links to classes, while classes themselves are objects with their own methods. Understanding the object model and how objects and classes work is crucial for mastering Ruby’s object-oriented programming paradigm.