Object-Oriented Programming (OOP) is a paradigm that focuses on organizing code into objects, each encapsulating data and behavior. Ruby is a versatile and dynamically-typed language that fully embraces OOP principles. In this comprehensive guide, we'll explore the fundamentals of OOP in Ruby, covering classes, objects, inheritance, polymorphism, encapsulation, and more.
Object-Oriented Programming is a programming paradigm that revolves around the concept of "objects." An object is an instance of a class, which is a blueprint defining the properties and behaviors shared by all instances.
OOP is built on four main principles:
In Ruby, a class is defined using the class keyword.
class Dog
def bark
puts "Woof!"
end
end
Objects are instances of classes. You create an object using the new method.
my_dog = Dog.new
my_dog.bark # Output: "Woof!"
The initialize method is a special method that gets called when an object is created.
class Dog
def initialize(name, age)
@name = name
@age = age
end
def bark
puts "Woof!"
end
end
my_dog = Dog.new("Buddy", 3)
puts my_dog.instance_variable_get(:@name) # Output: "Buddy"
Attributes store the state of an object. They are accessed using instance variables.
class Person
def initialize(name, age)
@name = name
@age = age
end
def introduce
puts "Hi, I'm #{@name}, and I'm #{@age} years old."
end
end
person = Person.new("Alice", 25)
person.introduce # Output: "Hi, I'm Alice, and I'm 25 years old."
Methods define the behavior of a class.
class Calculator
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
end
calc = Calculator.new
result = calc.add(5, 3) # Result: 8
Inheritance allows a class to inherit attributes and methods from another class.
class Animal
def eat
puts "Nom nom nom"
end
end
class Dog < Animal
def bark
puts "Woof!"
end
end
dog = Dog.new
dog.eat # Output: "Nom nom nom"
dog.bark # Output: "Woof!"
Polymorphism allows objects of different classes to be treated as objects of a common base class.
class Bird
def make_sound
puts "Tweet tweet"
end
end
class Duck < Bird
def make_sound
puts "Quack quack"
end
end
class Crow < Bird
# Inherits make_sound from Bird
end
duck = Duck.new
crow = Crow.new
duck.make_sound # Output: "Quack quack"
crow.make_sound # Output: "Tweet tweet"
Encapsulation involves bundling data and methods that operate on that data into a single unit (class).
class BankAccount
def initialize(balance)
@balance = balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount if amount <= @balance
end
def balance
@balance
end
end
account = BankAccount.new(1000)
account.withdraw(500)
puts account.balance # Output: 500
Modules provide a way to group related methods, classes, and constants together.
module MathOperations
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
end
class Calculator
include MathOperations
end
calc = Calculator.new
result = calc.add(7, 3) # Result: 10
Object-Oriented Programming in Ruby provides a powerful and flexible paradigm for building scalable and maintainable software. By understanding and applying the principles of encapsulation, abstraction, inheritance, and polymorphism, developers can design elegant and modular code. Ruby's dynamic nature and syntax make it an excellent language for embracing OOP concepts. As you continue to explore and practice OOP in Ruby, you'll unlock the full potential of this versatile programming paradigm. Happy coding!