OBJECT-ORIENTED PROGRAMMING (OOP)
object-oriented programming
ruby
oop
objects
class
polymorphism
attributes
encapsulation
modules
mixins
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects. In OOP, objects are instances of classes, which serve as blueprints for creating objects. Each object has properties (attributes or data fields) and behaviors (methods or functions).
In Ruby, OOP is deeply ingrained into the language's design. Here's a brief overview of how OOP works in Ruby:
In Ruby, OOP is deeply ingrained into the language's design. Here's a brief overview of how OOP works in Ruby:
- 1. CLASSESS
- Classes are the building blocks of Ruby's OOP paradigm. You define a class using the class keyword followed by the class name.
For example:class Person # Class definition end
2. OBJECTS
Objects are instances of classes. You create objects using the new method of a class.
For example:
person1 = Person.new
3. ATTRIBUTES
Attributes are characteristics of an object. In Ruby, you can define attributes using instance variables (@variable_name). You typically define attribute accessors using attr_reader, attr_writer, or attr_accessor.
For example:
class Person attr_accessor :name, :age end
4. METHODS
Methods are behaviors associated with objects. You define methods inside class definitions.
For example:
Methods are behaviors associated with objects. You define methods inside class definitions.
For example:
class Person
def greet
puts "Hello, I'm #{@name} and I'm #{@age} years old."
end
end5. POLYMORPHISM
Ruby supports polymorphism, allowing objects of different classes to respond to the same method in different ways. This is achieved through method overriding.
For example:
class Animal
def make_sound
puts "Some generic sound"
end
end
class Dog < Animal
def make_sound
puts "Woof!"
end
end
class Cat < Animal
def make_sound
puts "Meow!"
end
end
class Cow < Animal
def make_sound
puts "Moo!"
end
end
# Polymorphism in action
animals = [Dog.new, Cat.new, Cow.new]
animals.each do |animal|
animal.make_sound
endOutput:
Woof! Meow! Moo!
6. ENCAPSULATION
Ruby supports encapsulation by controlling access to attributes and methods using access modifiers (public, protected, private).
For example:
class Person
def initialize(name, age)
@name = name
self.age = age # Using the setter method for encapsulation
end
def age
@age
end
def age=(new_age)
if new_age >= 0
@age = new_age
else
puts "Age cannot be negative!"
end
end
end
person = Person.new("Alice", 30)
puts person.age # Output: 30
person.age = -5 # Trying to set negative ageOutput:
30 Age cannot be negative!
7. MODULES and MIXINS
Ruby allows you to define reusable code in modules, which can then be included in classes using the include keyword. This is a form of multiple inheritance called mixins.
For example:
module Swimmable
def swim
puts "#{self.class} is swimming"
end
end
class Fish
include Swimmable
end
class Duck
include Swimmable
end
fish = Fish.new
duck = Duck.new
fish.swim # Output: Fish is swimming
duck.swim # Output: Duck is swimmingNote: In this example, both Fish and Duck classes can now use the swim method defined in the Swimmable module, thanks to mixins.
Overall, OOP in Ruby provides a flexible and intuitive way to structure and organize code, making it particularly suitable for building complex applications.
Overall, OOP in Ruby provides a flexible and intuitive way to structure and organize code, making it particularly suitable for building complex applications.