OOPL with Ruby

Ranjan Bajracharya
3 min readFeb 18, 2018

The features of the object-oriented programming language include −

  • Inheritance
  • Data Encapsulation
  • Polymorphism
  • Data Abstraction

First of all what is Ruby Class ????

A class is a blueprint of data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object. Class id defined :-

class Box
# code
end

What is Ruby Objects ??

A class is the blueprint from which individual objects are created. We declare objects of a class using new keyword. Deceleration of object:-

box1 = Box.new
box2 = Box.new

Ruby is an object-oriented programming language, which means it manipulates programming constructs called objects. (Almost) everything in Ruby is an object!

What’s in a @name?

In Ruby, we use @ before a variable to signify that it's an instance variable. This means that the variable is attached to the instance of the class.

class Car
def initialize(make, model)
@make = make
@model = model
end
end

Properties

Inheritance is the process by which one class takes on the attributes and methods of another, and it’s used to express an is-a relationship.Simply it’s concept can be related to parent and child.

class ApplicationError  # parent class
def display_error
puts “Error! Error!”
end
end
class Error < ApplicationError #Error child class
end
err = Error.new
err.display_error

Some languages allow a class to have more than one parent, which is a model called multiple inheritance.But Ruby disallows it.

However, there are instances where you want to incorporate data or behavior from several classes into a single class, and Ruby allows this through the use of mixins. When a module is used to mix additional behavior and information into a class, it’s called a mixin. Mixins allow us to customize a class without having to rewrite code!

# Create your module here
module Debug
def whoAmI?
"print hello world"
end
end
class Hello
include Debug
end
class HelloWorld
include Debug
end
p=HelloWorld.new
p.whoAmI?

Ruby allows you to explicitly make some methods public and others private. Public methods allow for an interface with the rest of the program.

Private methods, on the other hand, are for your classes to do their own work undisturbed. They don’t want anyone asking them anything, so they make themselves unreachable!

private methods are just that: they're private to the object where they are defined. This means you can only call these methods from other code inside the object. Another way to say this is that the method cannot be called with an explicit receiver. You've been using receivers all along—these are the objects on which methods are called! Whenever you call object.method, object is the receiver of the method

attr_reader, attr_writer

class Person
def initialize(name)
@name = name
end
def name
@name
end
def name=(value) # sign in a method name.
@name = value
end
end

Well, no longer we need to define getter setter such a way.We can use attr_reader to access a variable and attr_writer to change it. If we write

class Person
attr_reader :name
attr_writer :name
def initialize(name)
@name = name
end
end

(That name= might look funny, but you're allowed to put an = sign in a method name. That's just a Ruby convention saying, "hey, this method sets a value!")

attr_accessor

If we want to both read and write a particular variable, there’s an even shorter shortcut than using attr_reader and attr_writer. We can use attr_accessor to make a variable readable and writeable in one fell swoop.

Module

  • toolbox that contains a set methods and constants.
  • a module can have no instances and subclasses.
  • module names are written in CapitalizedCamelCase.
  • purposes of modules is to separate methods and constants into named spaces.
  • use explicitly module with require
require 'module'

Data Encapsulation means that the internal representation of an object is hidden from the outside. Only the object can interact with its internal data.

Polymorphism is the provision of a single interface to entities of different types.

Data Abstraction is just a fancy way of saying that we are going to model our data to look like it’s real life equivalent.

--

--

Ranjan Bajracharya

MSP 2017. Graduation in computer science and information technology. Studying MBA.