100 Days of Learning Ruby: Day #4
Tags: 100daysofcode, ruby, beginners
Published: 2022-01-10 20:25:22

## OOP in Ruby (Part II)

Last time we learnt what is Object Oriented Programming and we started to learn how it is implemented in Ruby, today we will continue learning about OOP in Ruby.

- What is attr_accessor, attr_reader and attr_writer?
- Inheritance in Ruby


### Setup for Today

Create a new folder called Day_4, then create a new file with the name oop2.rb and finally open it up in a text editor of your choice.

```sh
cd 100_days_of_ruby/
mkdir Day_4 && cd Day_4/ && touch oop2.rb
codium .
```

### What is attr_accessor, attr_reader and attr_writer?

Last time we created the following class:

```ruby
class Dog

  def initialize()
    @age = 3
  end

  # return the age of the dog
  def GetAge()
    return @age
  end

  # set the age of the dog
  def SetAge(age)
    @age = age
  end

end
```

And we used the `SetAge` method to set the dog's age and the `GetAge` to retrieve the dog's age. But why create these methods ourselves when Ruby can do it for us.

To understand that let's firstly see that we can't just simply access the `age` attribute/variable but we need to call the `GetAge` or `SetAge` method.

For example add the above class to our file and then below that add the following lines of code:

```ruby
dog = Dog.new  # creating a new Dog object

puts dog.age   # trying to print the age of our object
```

If you try to run this code you will end up with an error saying `undefined method 'age'`. We cannot _access_ the attribute `age` without calling our GetAge method.

Let's modify our Dog class a bit:

```ruby
class Dog

  attr_accessor :age

  def initialize()
    @age = 3
  end
  
end

dog = Dog.new   # creating a new Dog object

puts dog.age    # printing the initialized age of our dog object

dog.age = 5     # modifying the age attribute

puts dog.age    # printing the new age of our dog object
```

Now if we run our program we can see that we have no errors. Also we can see that we removed our `SetAge` and `GetAge` methods and we added the line `attr_accessor :age`.

So what happened is that when we declare a class attribute with `attr_accessor` Ruby will automatically create a Getter and a  Setter method for that attribute. 

If we only wanna have a Getter for an attribute then we declare it with `attr_reader` and if we only wanna have a Setter for an attribute we declare it with `attr_writer`.

Add the following lines of code to our class to demonstrate the above:

```ruby
attr_reader :name
attr_writer :dob  # dob = date of birth

def initialize()
  @age = 3
  @name = 'Snoopy'
  @dob = '13-06-2003'
end
```

Now go ahead and print the `name` and the `dob` of the Dog object. You can print the attribute `name` cause it is declared with `attr_reader` but you can't print the attribute `dob` cause it's declared with `attr_writer`.

### Inheritance in Ruby

This blog is already kinda lengthy and I don't wanna over do it so I'm not gonna go over what Inheritance is, I'm just gonna show you how it's done in Ruby.

Add the newly created Animal class before the Dog class and change the class Dog slightly:

```ruby
class Animal

  def Eat(food)
    puts "I like to eat #{food}"
  end

  def Sleep()
    puts "I'm going to sleep."
  end

  def Drink()
    puts "I drink lots of water"
  end

end

class Dog < Animal
  attr_accessor :age
  attr_reader :name
  attr_writer :dob  # dob = date of birth

  def initialize()
    @age = 3
    @name = 'Snoopy'
    @dob = '13-06-2003'
  end
  
end
```

So in Ruby a class can inherit the methods and the attributes of another class with the symbol `<`. 
Here we can see that the class Dog inherits 3 methods from the class Animal. We can now modify our program again to use these functions from a Dog object.

Add the following to the end of our program:

```ruby
puts dog.Eat("Meat")
puts dog.Drink
puts dog.Sleep
``` 

We will end Day #4 here but again we haven't learnt everything about OOP in Ruby, I decided to cover more advanced OOP concepts a bit later, so some time in the series there is going to be a Part III. Next time we will learn about the Control Flow of our Ruby program (if statements, loops etc).

I was a bit hasty with this one cause I don't have much time to practice and learn ruby these days. That's also the reason that I don't create these blogs more often.

Anayways thanks for joining, I will see you next time!