Ruby on Rails - Object-Oriented Programming in Ruby
Introduction
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure and organize code. Ruby is a purely object-oriented language, making it a great choice for learning and applying OOP concepts. This guide will cover the basics of OOP in Ruby.
Key Points:
- OOP uses objects and classes to organize code.
- Ruby is a purely object-oriented language.
- This guide covers OOP concepts such as classes, objects, inheritance, encapsulation, and polymorphism in Ruby.
Classes and Objects
In Ruby, everything is an object, and objects are instances of classes. A class defines the blueprint for objects. Here is an example:
# Define a class
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def greet
"Hello, my name is #{@name} and I am #{@age} years old."
end
end
# Create an object
person = Person.new("Alice", 30)
puts person.greet # Output: Hello, my name is Alice and I am 30 years old.
In this example, a class Person
is defined with attributes and methods, and an object is created and used.
Inheritance
Inheritance allows a class to inherit the attributes and methods of another class. Here is an example:
# Define a superclass
class Animal
attr_accessor :name
def initialize(name)
@name = name
end
def speak
"Hello, I am an animal."
end
end
# Define a subclass
class Dog < Animal
def speak
"Woof! My name is #{@name}."
end
end
# Create an object of the subclass
dog = Dog.new("Buddy")
puts dog.speak # Output: Woof! My name is Buddy.
In this example, the Dog
class inherits from the Animal
class and overrides the speak
method.
Encapsulation
Encapsulation is the practice of hiding the internal state and behavior of an object and only exposing a public interface. Here is an example:
# Define a class with encapsulation
class BankAccount
def initialize(balance)
@balance = balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
if amount <= @balance
@balance -= amount
else
"Insufficient funds."
end
end
def balance
@balance
end
private
def calculate_interest
@balance * 0.05
end
end
# Create an object
account = BankAccount.new(100)
account.deposit(50)
account.withdraw(30)
puts account.balance # Output: 120
In this example, the internal state of the BankAccount
class is encapsulated, and only specific methods are exposed as the public interface.
Polymorphism
Polymorphism allows objects of different classes to respond to the same method call. Here is an example:
# Define a superclass
class Animal
def speak
"Hello, I am an animal."
end
end
# Define subclasses
class Dog < Animal
def speak
"Woof!"
end
end
class Cat < Animal
def speak
"Meow!"
end
end
# Create objects
animals = [Dog.new, Cat.new]
# Call the same method on different objects
animals.each do |animal|
puts animal.speak
end
# Output:
# Woof!
# Meow!
In this example, both the Dog
and Cat
classes inherit from the Animal
class and implement the speak
method, demonstrating polymorphism.
Modules
Modules in Ruby are used to group related methods and constants. They can be included in classes to share functionality. Here is an example:
# Define a module
module Greetable
def greet
"Hello!"
end
end
# Include the module in a class
class Person
include Greetable
end
# Create an object
person = Person.new
puts person.greet # Output: Hello!
In this example, the Greetable
module is defined and included in the Person
class, adding the greet
method to instances of Person
.
Conclusion
This guide introduced the basics of Object-Oriented Programming (OOP) in Ruby, covering concepts such as classes, objects, inheritance, encapsulation, polymorphism, and modules. With these fundamentals, you can start applying OOP principles to your Ruby programming projects.