Signup/Sign In

Ruby Classes

Ruby is a pure Object Oriented Programming Language. An object-oriented program consists of Classes and Objects. An object is an entity that serves as a container for data and also controls access to the data.

A class is like a blueprint that allows you to create objects and to create methods that relate to those objects. For example, you might use a Shape class to make different shapes like Rectangle, Square, Circle, and so on. An object is an instance of a class.

Class Hierarchy in Ruby

Class Hierarchy

There are many classes in Ruby. Not all the classes are included in this Hierarchy. Class Basic Object is the superclass of all the classes.

Checking Super Class in Ruby

We have set the local variable val to be a string. When we check the class of val, by calling the .class method, it tells us that the class of val is String. Then, when we checked the superclass of String, by calling the .superclass method it tells us Object, and so on.

The reason the last value is nil is because the class BasicObject has no superclass.

Defining Class in Ruby

A class in Ruby starts with the keyword class followed by the name of the class.

Creating a class in Ruby


Creating Objects in Ruby

Objects in ruby are created using the method new. new method belongs to the class Class.

obj1 = Shape.new

This is the syntax for creating object. Here obj1 is the object name and Shape is the class name.

Another example of Class:

Creating a objects in Ruby

In the above program we have created a class named Name.

The method initialize is special type of method which is called by the new method in the object creation. The initialize method accepts three arguments which is stored in the instance variables @first, @middle and @last whereas the variables first, middle and last are local variables. The inspect method gives the information about the class name and list of instance variables and their values.

This is the output of the program:

Creating a class & objects in Ruby

Another example using to_s method:

to_s (to string) method allows us to display the state of the object. State refers to all the data that is being stored in the class objects.

Creating a class & objects in Ruby

The output of the program is :

Creating a class & objects in Ruby

When we change the last line of program to print obj1. Here we are literally printing the object, let’s see the output after this change :

Creating a class & objects in Ruby

So, when the object is printed, the to_s method is called automatically. This method can be used to debug or test the code by displaying the state of the object at any particular time.


Ruby: Accessing Class and Object Attributes

We can also write methods to allow us to examine a single field or attribute of an object.

Accessing the attributes of a class

You have to define the method with the name of the attribute/field. Here we have created a method for the field first. The method is called using object name.

The output of the program is :

Accessing the attributes of a class

Likewise, we can examine each attribute of the object. We can do this in another simple way.

Accessing the attributes of a class

The attributes can be examined by the following statement attr_Reader :first, :middle, :last. This method returns the values of the attributes of the object which is printed using print method.

The output of the program is:

Accessing the attributes of a class


Making the attributes Writable

The attributes can be made writable / modified by the following statement:

attr_writer :first, :middle, :last

Writing the attributes of a class

Here, we have modified the instance variables of the object obj1. The output of the program is :

Writing the attributes of a class

Here, you can see that the first, middle and last values were modified and printed.