A Beginner's Guide to understanding Object Oriented Programming (OOP) in Ruby
Tags: ruby, beginners, tutorial, webdev
Published: 2023-11-14 15:35:13

In this guide, we'll delve into the key principles of Object Oriented Programming in Ruby, exploring classes, objects, inheritance, encapsulation, and polymorphism in Ruby.

## Table of Contents
&nbsp;1. [What is Object Oriented Programming in Ruby](#what-is-object-oriented-programming-in-ruby)
&nbsp;2. [Understanding Classes and Objects in Ruby](#understanding-classes-and-objects-in-ruby)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.1. [Classes: Blueprints for Objects](#classes-blueprints-for-objects)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a. [Understanding Classes in Ruby](#understanding-classes-in-ruby)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b. [Creating instances from a Class](#creating-instances-from-a-class)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c. [Using Methods of Objects](#using-methods-of-objects)
&nbsp;3. [Inheritance in Ruby: Sharing Traits](#inheritance-in-ruby-sharing-traits)
&nbsp;4. [Encapsulation in Ruby: Keeping Secrets](#encapsulation-in-ruby-keeping-secrets)

&nbsp;5. [Polymorphism in Ruby: Many Faces](#polymorphism-in-ruby-many-faces)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.2 [Method Overriding in Ruby](#method-overriding-in-ruby)
&nbsp;6. [Abstraction in Ruby](#abstraction-in-ruby)
&nbsp;7. [Conclusion](#conclusion)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7.3. [Unveiling the Power of Classes and Objects](#unveiling-the-power-of-classes-and-objects)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7.4. [Embracing the Dynamic Force of Polymorphism](#embracing-the-dynamic-force-of-polymorphism)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7.5. [The Ongoing Coding Odyssey](#the-ongoing-coding-odyssey)


## What is Object Oriented Programming in Ruby

![Image of object oriented programming in Ruby](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvkvqgdtrxh1ynmvbq6n.png)
Object-Oriented Programming (OOP) is like organizing your code using building blocks called **"objects."** These objects have special powers—they can store information and perform actions. In Ruby, a programming language known for its simplicity, OOP is a key feature.

## Understanding Classes and Objects in Ruby

![understanding classes and objects in Ruby](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb24v0p2jwcznr73fm3g.png)

### Classes in Ruby: Blueprints for Objects

In OOP, a class acts as a blueprint or template for creating objects. It defines the structure and behavior that the objects based on it will possess. These objects are called instances of the class. Think of a class as a recipe for making pasta, and the instances as the various dishes of pasta that is being made.

#### Understanding Classes in Ruby

A class typically includes the following components:

1. **Class Declaration:** It starts with the keyword `class` followed by the name of the class. Class names in Ruby are typically written in CamelCase.

    ```ruby
    class Dog
    ```

2. **Constructor Method (`initialize`):** The `initialize` method is a special method that gets called when a new object is created from the class. It's used to set up the initial state of the object.

    ```ruby
    def initialize(name, age)
      @name = name
      @age = age
    end
    ```

    In this example, `name` and `age` are parameters passed when creating a new `Dog` object.

3. **Instance Variables (`@name`, `@age`):** These variables store the state of the object. They have an `@` symbol before their names, indicating that they belong to the instance of the class.

    ```ruby
    @name = name
    @age = age
    ```

4. **Methods:** These are functions defined within the class. They represent the behavior or actions that objects created from the class can perform.

    ```ruby
    def bark
      puts "Woof, woof!"
    end

    def details
      puts "Name: #{@name}, Age: #{@age}"
    end
    ```

    Here, `bark` and `details` are methods that a `Dog` object can call to perform specific actions.

#### Creating instances from a Class

Once a class is defined, you can create instances or objects based on that class. Objects are instances of a class, and each object has its own state and behavior.

```ruby
dog1 = Dog.new("Buddy", 3)
dog2 = Dog.new("Max", 5)
```

Here, `dog1` and `dog2` are instances of the `Dog` class. The `initialize` method is called automatically when these objects are created, setting up their initial state.

#### Using Methods of Objects

Objects created from a class can invoke its methods:

```ruby
dog1.bark
dog2.details
```

In this example, `dog1` barks, and `dog2` provides details. Each object can use the methods defined in the class, but the specific details might be different based on the state of the object.

##Inheritance in Ruby: Sharing Traits

Inheritance is like passing on traits from parents to children. In Ruby, a class can inherit traits from another class. Let's see how:

```ruby
class Animal
  def initialize(name)
    @name = name
  end

  def speak
    puts "Some generic sound"
  end
end

class Cat < Animal
  def speak
    puts "Meow!"
  end
end
```

Here, Cat is like a child inheriting traits from its parent (Animal). It can still do everything an Animal can, but it has its own unique way of speaking.

```ruby

generic_animal = Animal.new("Generic Animal")
generic_animal.speak

my_cat = Cat.new("Whiskers")
my_cat.speak
```

Just like how a cat inherits traits from an animal, our `my_cat` speaks in a cat-like way.

##Encapsulation in Ruby: Keeping Secrets

Encapsulation is like putting your code in a secret box. It keeps things organized and prevents others from messing with your stuff. Let's see how it works:

```ruby

class Book
  attr_accessor :title, :author

  def initialize(title, author)
    @title = title
    @author = author
  end

  def details
    "Title: #{@title}, Author: #{@author}"
  end
end
```

Here, Book is like a secret box. It has methods (details) that let you see what's inside, but you can't directly mess with the title or author unless you use the provided methods.

```ruby

my_book = Book.new("Ruby Programming", "John Doe")
puts my_book.details

my_book.title = "Mastering Ruby"
puts my_book.details
```

You can see what's inside the `my_book` box using the `details` method. And you can change the title, but only through the method provided.


##Polymorphism in Ruby: Many Faces

Polymorphism is a fascinating aspect of OOP (Object Oriented Programming) that allows objects to be treated as instances of their parent class, even when they are instances of a child class. It's like using different things interchangeably. In Ruby, polymorphism is often seen through method overriding.

###Method Overriding in Ruby

Method overriding occurs when a child class provides a specific implementation for a method that is already defined in its parent class. This allows objects of the child class to be used in a way that is interchangeable with objects of the parent class.

Let's illustrate this with an example:

```ruby
class Animal
  def speak
    puts "Some generic sound"
  end
end

class Cat < Animal
  def speak
    puts "Meow!"
  end
end
```

Here, `Cat` is a child class of `Animal`. The `speak` method is overridden in the `Cat` class to provide a cat-specific sound. Now, both `Animal` and `Cat` objects can be used interchangeably.

```ruby
generic_animal = Animal.new
generic_animal.speak  # Outputs: Some generic sound

my_cat = Cat.new
my_cat.speak          # Outputs: Meow!
```

Even though `generic_animal` and `my_cat` are instances of different classes, they both respond to the `speak` method. This is polymorphism in action.

Polymorphism in Ruby allows objects to be treated as instances of their parent class, enabling flexibility and interchangeability in the use of objects. It's a powerful concept that enhances the adaptability and extensibility of your code.

##Abstraction in Ruby

In Ruby, abstraction is about simplifying complex code by focusing on what objects do rather than how they do it. It's achieved through abstract classes or interfaces.

1. **Abstract Classes:**
   - Use the `abstract_class` gem or convention.
   - Define abstract methods without implementations.
   - Subclasses must provide implementations.

   ```ruby
   require 'abstract_class'

   class Animal
     abstract_class

     def speak
       raise NotImplementedError, 'Subclasses must implement speak'
     end
   end

   class Dog < Animal
     def speak
       puts 'Woof!'
     end
   end

   class Cat < Animal
     def speak
       puts 'Meow!'
     end
   ```

2. **Interfaces in Ruby:**
   - Use modules to create interfaces.
   - Modules define methods classes must implement.

   ```ruby
   module Speakable
     def speak
       raise NotImplementedError, 'Classes must implement speak'
     end
   end

   class Dog
     include Speakable

     def speak
       puts 'Woof!'
     end
   end

   class Cat
     include Speakable

     def speak
       puts 'Meow!'
     end
   ```

Abstraction simplifies code, making it more modular and easier to maintain by focusing on essential features rather than implementation details.


## Conclusion

In our exploration of Ruby's Object-Oriented Programming (OOP), we've traversed the landscapes of classes, objects, inheritance, encapsulation, polymorphism, and the dynamic realm of class variables. As we conclude this comprehensive journey, let's reflect on the essence of our coding odyssey.

### Unveiling the Power of Classes and Objects

**Classes as Architects:** In Ruby, classes transcend their role as blueprints; they are architects designing living, breathing entities. With each class, we create not just structures but dynamic realms filled with unique and captivating objects.

**Objects as Dynamic Entities:** Every object born from a class is a dynamic entity with its own story, featuring distinct attributes and action-packed methods. Like characters in a novel, these objects add depth and richness to our code, making it not just functional but expressive.

### Embracing the Dynamic Force of Polymorphism

**Polymorphism as a Symphony:** Polymorphism emerges as a symphony conductor, orchestrating a harmonious collaboration of methods. Through method overriding, our code adapts, evolves, and performs dynamic feats, akin to a mesmerizing symphony where each instrument plays a unique part.

### The Ongoing Coding Odyssey

As we conclude this exploration, remember that coding is an ongoing odyssey. The knowledge gained here is not just a toolkit; it's a compass guiding you through the vast and ever-expanding landscape of Ruby's OOP. With every line of code, you have the opportunity to craft intricate narratives, build interconnected universes, and truly master the art of Object-Oriented Programming.

Happy Coding!

Let's connect on [LinkedIn](www.linkedin.com/in/dumebi-okolo). 
Follow me on [X (formerly Twitter)](https://x.com/DumebiTheWriter?t=TefjPS_lL-_qVugq9i8mwA&s=09) 

#aiAssistedArticle
[Cover photo](https://morioh.com/a/52db7b655ffd/comprehensive-guide-to-object-oriented-programming-oop-in-ruby)
[Image: OOP in Ruby](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.scaler.com%2Ftopics%2Foops-in-ruby%2F&psig=AOvVaw3N62FMNHPmRbtzYAni95GK&ust=1700061770887000&source=images&cd=vfe&opi=89978449&ved=2ahUKEwi2n-_g5cOCAxWjticCHY4aBeQQr4kDegQIARBh)
[Image: understanding classesand objects in Ruby](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.codesdope.com%2Fruby-oop%2F&psig=AOvVaw3N62FMNHPmRbtzYAni95GK&ust=1700061770887000&source=images&cd=vfe&opi=89978449&ved=0CBQQjhxqFwoTCOiOr-Tlw4IDFQAAAAAdAAAAABAS)