While learning Ruby you will hear everything in Ruby is an object. This post will cover objects, classes, class variables, instance variables, and self.
Object Orientated Program (OOP) is a piece of code use to mimic real world situations.
A class is a blueprint that for something and an object is a copy of that blueprint.
Classes are like cookie cutters. A cookie cutter can stamp out many different types of cookies. Gingerbread, sugar cookies, and chocolate chip cookies (my personal favorite). A cookie is an object. You can have many cookies (objects) and each cookie can have different characteristics (properties).
Initializing a new object
Start with creating a new file called cookie.rb and inside that file we will define a cookie class and initializing a message that a cookie was created. When you add an initializing method to your cookie class it will be included every time you create a new cookie. The initializing method is a constructor.
In your IRB terminal if you run:
You will have created a new cookie object <Cookie: OxOO7f8ed418ebeO>
Let’s create a new cookie with a name.
You now have a cookie object with the name ‘Sugar Cookie’.
The @name is an instance variable. The @name variable will belong to the Cookie class. A cookie name is passed in as an argument.
A method is a behavior think it as a verb. A verb describes an action that does something. We are going to create a method that decorates our cookie with a specific color.
run your cookie.rb file
Our cookie now has a name and is decorated but what if we wanted to see the cookie name?
We can try
We get a (NoMethodError). Which means we need to create a method for getting the name because either it doesn’t exist or we don’t have any access to the name method.
Let’s create a method that will return the name of the cookie called get_name.
We now get ‘Sugar Cookie’ as the name for our cookie.
The get_name is known as a getter method. What if we wanted to change the name of my cookie to Super Duper Sugar Cookie? We would have to add a setter method called change_name and pass in a new name as an argument.
Now we can change my sugar_cookie name to Super Duper Sugar Cookie
sugar_cookie.change_name("Super Duper Sugar Cookie")
returns ‘Super Duper Sugar Cookie’
A shortcut to set a setter and getter method is to use the attr_accessor method.
Let’s refactor this code to include the attr_accessor method for name.
The attr_accessor method takes a symbol as an argument, which is used to create a getter and setter methods. One line of code will replace the get_name and change_name methods.
We can now read (getter method) the cookie’s name by using: sugar_cookie.name
We can also write a new name (setter method) for sugar_cookie
sugar_cookie.name = "Super Duper Sugar Cookie"
Let’s add more properties to our cookie. When a cookie gets initialized we want it to have a description and the day it is available for purchase.
We add a getter and setter method for description and available.
Then add an info method to get the information about our cookie.
run your cookie.rb file
Death by chocolate is made with dark chocolate cookie with chocolate chips topped with a dark ganche and is sold on Fridays.
What if we want to change our cookie description or the day that is sold? We would have to add a method to change that information.
Calling methods with self
We don’t want to create new variable but we want to call on the setter method. Using self.name will call on the @name variable we used originally to set the name of our cookie.
To be consistent we also want to change the info method to reflect self for the instance variables we are accessing.
Class methods that are called directly on the class itself, without instantiating any objects.
We have a class variable called @@number_of_cookies, which starts at 0. Every time we create a new cookie (initialize) the number of cookies gets increased by 1. We can also access the class variable from within our instance method. This is an example of using a class variable and a class method to keep track of class level information that is specific to the class.
Object Orientated Program can be a very complicated to understand. Try and create some of your own classes and practice with different ideas. Try applying this lesson to creating cars, houses, or people. The more practice you get the better you will understand OOP.