Ruby Classes & Objects

To be considered a "pure" Object-Oriented languages the following qualities must be satisfied:

  1. Encapsulation/Information Hiding
  2. Inheritance
  3. Polymorphism/Dynamic Binding
  4. All pre-defined types are Objects
  5. All operations performed by sending messages to Objects
  6. All user-defined types are Objects

More details on these features in Programming Language Comparison.

Ruby, satisfies them all.

In class-based OO languages, a program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.

Take the example of any vehicle. It comprises wheels, horsepower, and fuel or gas tank capacity. These characteristics form the data members of the class Vehicle. You can differentiate one vehicle from the other with the help of these characteristics.

A vehicle can also have certain behaviors, such as halting, driving, and speeding. Even these functions form the data members of the class Vehicle. You can, therefore, define a class as a combination of characteristics and functions.

A class Vehicle can be defined as:

      class Vehicle
        def no_of_wheels(number); end
        def horsepower(number); end
        def tank_type(string); end
        def speed_rating(reverse, forward); end
        def drive(direction, rate); end
        def halt(urgency); end

By assigning different values to these data members, you can form several instances of the class Vehicle. For example, an airplane has three wheels, horsepower of 1,000, fuel as the type of tank, and a capacity of 100 liters. In the same way, a car has four wheels, horsepower of 200, gas as the type of tank, and a capacity of 25 litres.

Defining a Class in Ruby:

To implement object-oriented programming by using Ruby, you need to first learn how to create objects and classes in Ruby.

A class in Ruby starts with the keyword class followed by the name of the class. The name should always be in initial capitals. The class Customer can be displayed as:

      class Customer

You terminate a class by using the keyword end. All the data members in the class are between the class definition and the end keyword.

And of course we have the new method:

      Klass = do
         def instance_method
            puts "Hi from instance"

Variables in a Ruby Class:

Ruby provides four types of variables:


Using the class variable @@no_of_customers, you can determine the number of objects that are being created. This enables in deriving the number of customers.

      class Customer
         @@no_of_customers = 0

Notice: Global and Class variables are dangerous, and must be carefully used.

Creating Objects in Ruby using new Method:

Objects are instances of the class. You will now learn how to create objects of a class in Ruby. You can create objects in Ruby by using the method new of the class.

The method new is a unique type of method, which is predefined in the Ruby library. The new method belongs to the class methods.

Here is the example to create two objects cust1 and cust2 of the class Customer:

      cust1 =
      cust2 =

Here, cust1 and cust2 are the names of two objects. You write the object name followed by the equal to sign (=) after which the class name will follow. Then, the dot operator and the keyword new will follow.

Custom Method to create Ruby Objects :

You can pass parameters to method new and those parameters can be used to initialize class variables.

When you plan to declare the new method with parameters, you may want to declare the method initialize at the time of the class creation.

The initialize method is a special type of method, which will be executed when the new method of the class is called with parameters.

Here is the example to create initialize method:

      class Customer
         @@no_of_customers ||=0
         def initialize(name)
            @name = name
            @@no_of_customers += 1
         def self.no_of_customers
      ['Walter','Jorge','Stan','Bruce'].each do |name|
      puts Customer.no_of_customers   # 4

In this example, you declare the initialize method with name as local variable. Here def and end are used to define a Ruby method initialize.

In the initialize method, you pass on the value of the local variable to the instance variable @name. Here local variable hold the value that is passed along with the new method.

Now you can create objects as follows:

      cust1 ="John")
      cust2 ="Paul")

Member Functions in Ruby Class:

In Ruby, functions are called methods. Each method in a class starts with the keyword def followed by the method name.

The method name is always preferred in lowercase letters. You end a method in Ruby by using the keyword end.

Here is the example to define a Ruby method:

      class Sample
         def function
            statement 1
            statement 2

Here statement 1 and statement 2 are part of the body of the method function inside the class Sample. These statments could be any valid Ruby statement. For example we can put a method puts to print Hello Ruby as follows:

      class Sample
         def hello
            puts "Hello Ruby!"

Now in the following example create one object of Sample class and call hello method and see the result:

      class Sample
         def hello
            puts "Hello Ruby!"
      # Now using above class to create objects
      object =

This will produce following result:

      Hello Ruby!