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:EncapsulationPolymorphismInheritanceAbstraction
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,
class Greeter
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,
- java = Java.new(“John”)
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.
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:
#!/usr/bin/ruby -w
# define a class
class Circle
# constructor method
def initialize(r)
@radius = r
end
# instance method
def getArea
3.14 * @radius * @radius
end
end
# create an object
circle = Circle.new(2)
# call instance methods
a = circle.getArea()
puts "Area of the box is : #{a}"
Output:
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:
parentClass < subClass
Example:
#!/usr/bin/ruby
class Parent
def initialize
puts "Parent class created"
end
end
class Child < Parent
def initialize
super
puts "Child class created"
end
end
Parent.new
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:
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:
#!/usr/bin/ruby
class Parent
def initialize
puts "Parent is created"
end
end
Parent.new
Leave a Reply