Ruby Classes and Objects

Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages include:

  • Data Encapsulation
  • Data abstraction
  • polymorphism
  • inherit
An object-oriented program that involves classes and objects. A class is a blueprint created by an individual object. In object-oriented terminology, your bike is an example of a bicycle class.

In the case of a vehicle, it includes wheels, horsepower, fuel or gas tank capacity. These attributes form the data members of the Vehicle class. With these attributes you can distinguish a vehicle from other vehicles.

Vehicles can also contain specific functions such as halting, driving, and speeding. These functions form the data members of the Vehicle class. Therefore, you can define a class as a combination of properties and functions.


Class Vehicle { Number no_of_wheels Number horsepower Characters type_of_tank Number Capacity Function speeding { } Function driving { } Function halting { } }

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

Defining classes in Ruby

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

In Ruby, classes always start with the keyword class followed by the name of the class. The first letter of the class name should be capitalized. The class Customer looks like this:

class Customer end

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

Variables in the Ruby class

Ruby provides four types of variables:

  • Local variables: Local variables are variables defined in a method. Local variables are not available outside of the method. In the following chapters, you will see more details about the method. Local variables start with a lowercase letter or _.
  • Instance Variables: Instance variables can be used across methods in any particular instance or object. This means that instance variables can change from object to object. The instance variable places a symbol (@) before the variable name.
  • Class variables: Class variables can be used across different objects. A class variable belongs to a class and is a property of the class. Class variables place symbols (@@) before the variable name.
  • Global Variables: Class variables cannot be used across classes. If you want to have a variable that can be used across classes, you need to define a global variable. Global variables always start with a dollar sign ($).


Using the class variable @@no_of_customers, you can determine the number of objects being created so that you can determine the number of customers.


class Customer @@no_of_customers=0 end

Create objects in Ruby using the new method

An object is an instance of a class. Now you will learn how to create objects of a class in Ruby. In Ruby, you can create objects using the class's method new .

Methods new is a unique method that is predefined in the Ruby library. The new method belongs to the class method.

The following example creates two objects of class Customer cust1 and cust2:

cust1 = Customer. new cust2= Customer. new

Here, cust1 and cust2 are the names of the two objects. The object name is followed by an equal sign (=) followed by the class name, followed by the dot operator and the keyword new.

Custom methods to create Ruby objects

You can pass arguments to method new , which can be used to initialize class variables.

When you want to declare a new method with parameters, you need to declare the method initialize while creating the class.


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

The following example creates the initialize method:


class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end end

In this example, you can declare an initialize method with id, name, addr as a local variable. Here, def and end are used to define the Ruby method initialize. In the following chapters, you will learn more about the methods.

In the initialize method, pass the values of these local variables to the instance variables @cust_id, @cust_name, and @cust_addr. Here, the value of the local variable is passed along with the new method.

Now, you can create an object like this:

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire street, Khandala")

Member functions in the Ruby class

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

Method names always begin with lowercase letters. In Ruby, you can end a method with the keyword end.

The following example defines a Ruby method:

class Sample def function statement 1 statement 2 end end

Here, statement 1 and statement 2 are part of the body of the method function in the class Sample . These statements can be any valid Ruby statement. For example, we can use the method puts to output Hello Ruby as follows:

class Sample def hello puts "Hello Ruby!" end end

The following example creates an object of class Sample and calls the hello method:

#!/usr/container/ruby class Sample def hello puts "Hello Ruby!" end end # Use the above class to make an object object = Sample. new object.hello

This will produce the following results:

Hello Ruby!

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.