Ruby OOPs

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby OOPs Concept

Ruby is a true object oriented language which can be embedded into Hypertext Markup Language. Everything in Ruby is an object. All the numbers, strings or even class is an object. The whole Ruby language is basically built on the concepts of object and data.

OOPs is a programming concept that uses objects and their interactions to design applications and computer programs.

Following are some basic concepts in OOPs:

  • Encapsulation
  • Polymorphism
  • Inheritance
  • Abstraction

Encapsulation: It hides the implementation details of a class from other objects due to which a class is unavailable to the rest of the code. Its main purpose is to protect data from data manipulation.

Polymorphism: It is the ability to represent an operator or function in different ways for different data input.

Inheritance: It creates new classes from pre defined classes. New class inherit behaviors of its parent class which is referred as superclass. In this way, pre defined classes can be made more reusable and useful.

Abstraction: It hides the complexity of a class by modelling classes appropriate to the problem.


Ruby Class

Ruby class defines blueprint of a data type. It defines what does that class name means.

A class is defined with a class keyword followed by the class name and is ended with end keyword.

Conventionally, class name must begin with a capital letter. Class name with more than one word run together with each word capitalized and no separating characters.

Creating Class

Example:

We will create a class Java with following command,

  1. class Greeter  

Ruby Oops concept 1
 

A new class Java is created. The @name is an instance variable available to all the methods of the Java class. It is used by say_welcome and say_bye.

Ruby Objects

In Ruby, everything is an object. When we create objects, they communicate together through methods. Hence, an object is a combination of data and methods.

To create an object, first, we define a class. Single class can be used to create many objects. Objects are declared using newkeyword.

Creating Object

Example:

We have a class named Java. Now, let's create an object java and use it with following command,

  1. java = Java.new("John")  

Ruby Oops concept 2

nce java object is created, it will use John as the name.

Ruby Methods

Methods are functions which are defined inside the body of a class. Data in Ruby is accessible only via methods. There is a follow path in which Ruby looks when a method is called. To find out the method lookup chain we can use ancestors method.

Defining Method

A method is defined with def keyword and ends with end keyword.

We are defining a method name which will display the following message.

Ruby Oops concept 3

The def keyword starts the definition of method name. Then we write body of the mehtod. Last line end indicates that method is defined.

Instance Methods

The instance methods are also defined with def keyword and they can be used using a class instance only.

Example:

Output:

Ruby Oops concept 4

Ruby Inheritance

In inheritance, we create new classes using pre defined classes. Newly created classes are called derived classes and classes from which they are derived are called base classes. With inheritance, a code can be reused again which reduces the complexity of a program.

Ruby does not support multiple levels of inheritance. Instead it supports mixins.

In Ruby, < character is used to create a subclass. The syntax is shown below:

  1. parentClass < subClass  

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. class Parent   
  4.   
  5.     def initialize   
  6.         puts "Parent class created"   
  7.     end   
  8. end   
  9.   
  10. class Child < Parent   
  11.   
  12.    def initialize   
  13.        super   
  14.        puts "Child class created"   
  15.    end   
  16. end   
  17.   
  18. Parent.new   
  19. Child.new  

In the above example, two classes are created. One is base Parent class and other is derived Child class.

The super method calls the constructor of the Parent class.

From the last two line, we instantiate both the classes.

Output:

Ruby Oops concept 5
 

In the output, first the Parent class is created, derived Child class also calls the constructor of its parent class and then Child class is created.

Ruby Constructor

A constructor is automatically called when an object is created. They do not return any values. In Ruby, they are called initialize.

A constructor's main purpose is to initiate the state of an object. They can't be inherited. The parent object constructor is called with super method.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. class Parent   
  4.   
  5.     def initialize   
  6.         puts "Parent is created"   
  7.     end   
  8.   
  9. end   
  10.   
  11. Parent.new 

Output:

  Ruby Oops concept 6