Home » Ruby OOPs

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,

    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 new keyword.

    Creating Object

    Example:

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

    Ruby Oops concept 2

    Once 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:

    Example:

    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:

    Output:

    Ruby Oops concept 6


    You may also like