DEV Community

Dorthy Thielsen
Dorthy Thielsen

Posted on

Learning OO Ruby

This week I started learning about OOP in Ruby. At first when the idea of classes and objects were introduced in a very round about lecture, I was a little overwhelmed. However after just reading about it, it became so much easier and intuitive. Goes to show you that sometimes you need a different explanation from a different source. I will note that the instructor said they weren't feeling well, so that is probably why the lecture was unsuccessful for myself.

First we were introduced to writer/setter and reader/getter methods along with @instance_variables. It was great to finally have a way to call a variable outside of the method without making a global variable, which I have been told to never do. Basically the writer method sets up our instance variable. It takes a value and writes it into a variable or takes in an argument and sets that argument equal to a variable. You are setting a property to take parameters that will be used in your instance variable by each instance of the class. The reader method then returns the stored information of an instance variable. This is the part that allows us to call the instance variable outside the method anywhere in our program. Initially they had us writing each step out in a very long way
ex.

class Person

  def initialize(name)
    @name = name
  end

  def name
    @name
  end

  def name=(new_name)
    @name = new_name
  end

end
Enter fullscreen mode Exit fullscreen mode

When they were teaching this I immediately thought, "This seems like such a long way to write this code. There must be another way." To my relief, there is a simpler way to write all of this. Riding in on a noble steed was the attr_accessor, attr_reader, and attr_writer. This allows you to create attributes for each instance of your objects in one line of code instead of writing each reader and writer method yourself. Wouldn't you much rather write:

class Book
  attr_accessor :author, :page_count, :genre
  attr_reader :title 

  def initialize(title)
    @title=title
  end

end
Enter fullscreen mode Exit fullscreen mode

Rather than write all of this:

class Book

  def initialize(title)
    @title = title
  end

  def title
    @title
  end

  def author=(author)
    @author = author
  end

  def author
    @author
  end

  def page_count=(num)
    @page_count = num
  end

  def page_count
    @page_count
  end

  def genre=(genre)
    @genre = genre
  end

  def genre
    @genre
  end

end
Enter fullscreen mode Exit fullscreen mode

I think everyone would answer that the first way looks so much better and less repetitive. Here we can replace six methods and combine them into one in the attr_accessor which creates the writer and reader methods for you. Also instead of having to write:

class Book

  def initialize(title)
    @title = title
  end

  def title
    @title
  end
end
Enter fullscreen mode Exit fullscreen mode

You can turn the reader method into an attr_reader and have all the code fit on one line. Easy peasy. Here is another example of utilizing all of these methods and also adding in some functionality into everything too.

class Shoe
  attr_accessor :color, :size, :material, :condition
  attr_reader :brand

  def initialize(brand)
    @brand=brand
  end

  def cobble
    puts "Your shoe is as good as new!"
    @condition = "new"
  end
end
Enter fullscreen mode Exit fullscreen mode

I am excited to learn more about OOP and how to better write code.

Discussion (0)