OOP Concepts in Ruby
Object-Oriented Programming (OOP) is a way of organizing code around objects, which represent real-world entities. In Ruby, everything is an object—including numbers, strings, arrays, and even classes.
In OOP, a class is a blueprint, and an object is an instance of that blueprint. For example, if Vehicle is a class, then car1 and car2 are objects created from it—each with its own attribute values.
A class defines attributes (data) and methods (behavior). An object is created from a class using .new.
Basic Class Syntax
class ClassName
end
Ruby uses the initialize method as a constructor. It runs automatically when you create an object using .new.
class Vehicle
def initialize(make, model)
@make = make
@model = model
end
end
Creating Objects
car1 = Vehicle.new("Toyota", "Camry")
car2 = Vehicle.new("Honda", "Accord")
In Ruby, a method returns the last evaluated expression, so return is usually optional.
class Vehicle
def initialize(make, model)
@make = make
@model = model
end
def make
@make
end
def model
@model
end
end
Instead of accessing instance variables directly, you update values through methods.
class Vehicle
def initialize(make, model)
@make = make
@model = model
end
def model
@model
end
def change_model(new_model)
@model = new_model
end
end
car = Vehicle.new("Toyota", "Camry")
car.change_model("Corolla")
puts car.model
Ruby provides built-in helpers to create getter and setter methods.
attr_reader→ getter onlyattr_writer→ setter onlyattr_accessor→ getter + setter
class Vehicle
attr_accessor :make, :model
def initialize(make, model)
@make = make
@model = model
end
end
car = Vehicle.new("Toyota", "Camry")
car.model = "Corolla"
puts car.model
Ruby supports multiple variable types.
Instance Variables (@var)
Belong to a single object.
Class Variables (@@var)
Shared by all objects of a class.
Global Variables ($var)
Accessible everywhere (not recommended for most programs).
Class variables are shared across instances. Use class methods to access them.
class Vehicle
@@count = 0
def initialize(make, model)
@make = make
@model = model
@@count += 1
end
def self.count
@@count
end
end
Vehicle.new("Toyota", "Camry")
Vehicle.new("Honda", "Accord")
puts Vehicle.count
Inheritance allows a child class to reuse and extend behavior from a parent class.
class Device
def description
puts "This is a general device."
end
end
class Laptop < Device
def description
puts "This is a laptop."
end
end
Laptop.new.description
Use super to call the parent method.
class Device
def description
puts "This is a general device."
end
end
class Laptop < Device
def description
puts "This is a laptop."
super
end
end
Laptop.new.description
Ruby doesn’t support multiple inheritance directly, but you can reuse behavior using modules.
Using include (instance methods)
module Features
def feature_list
puts "Supports Wi-Fi and Bluetooth."
end
end
class Laptop
include Features
end
Laptop.new.feature_list
Using extend (class methods)
module Features
def feature_list
puts "This class supports advanced features."
end
end
class Laptop
extend Features
end
Laptop.feature_list
Ruby controls access at the method level, using:
public(default)privateprotected
Public and Private Example
class Device
def display_info
greet
puts "Device ready."
end
private
def greet
puts "Hello!"
end
end
Device.new.display_info
Polymorphism means the same method can behave differently depending on the object.
Polymorphism via Inheritance
class Instrument
def play
puts "Playing an instrument"
end
end
class Guitar < Instrument
def play
puts "Playing the guitar"
end
end
class Piano < Instrument
def play
puts "Playing the piano"
end
end
[Instrument.new, Guitar.new, Piano.new].each(&:play)
Polymorphism via Duck Typing
Ruby cares about what an object can do, not what it is.
class Store
def order(customer)
customer.payment_type
customer.discount
end
end
class RegularCustomer
def payment_type; puts "Paying with card"; end
def discount; puts "No discount"; end
end
class PremiumCustomer
def payment_type; puts "Paying with points"; end
def discount; puts "10% discount"; end
end
store = Store.new
store.order(RegularCustomer.new)
store.order(PremiumCustomer.new)
freeze makes an object immutable.
item = "Laptop"
item.freeze
puts item.frozen? # true
Ruby is a fully object-oriented language where classes, objects, inheritance, modules, and polymorphism are core tools for writing maintainable code. By combining:
- classes and constructors (
initialize) - accessors (
attr_accessor) - inheritance (
<andsuper) - modules (
includeandextend) - access control (
public,private,protected) - polymorphism (inheritance + duck typing)
…you can design clean, scalable Ruby applications.