A cookie is an object

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.

Screen Shot 2016-06-20 at 11.09.54 AM

In your IRB terminal if you run:
ruby cookie.rb

You will have created a new cookie object <Cookie: OxOO7f8ed418ebeO>

Screen Shot 2016-06-20 at 10.04.25 AM

Instance Variable

Let’s create a new cookie with a name.

Screen Shot 2016-06-20 at 11.11.44 AM

run  ruby cookie.rb

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.


Screen Shot 2016-06-20 at 10.12.53 AM


Instance Methods

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.

Screen Shot 2016-06-20 at 11.14.10 AM

run your cookie.rb file

Screen Shot 2016-06-20 at 10.36.27 AM

Our cookie now has a name and is decorated but what if we wanted to see the cookie name?

We can try

puts sugar_cookie.name

Screen Shot 2016-06-20 at 10.42.32 AM

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.

Screen Shot 2016-06-20 at 11.15.12 AM


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.

Screen Shot 2016-06-20 at 11.16.31 AM

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.

Screen Shot 2016-06-20 at 11.20.53 AM

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.

Screen Shot 2016-06-20 at 12.56.16 PM

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

Screen Shot 2016-06-20 at 1.13.39 PM

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.

Screen Shot 2016-06-20 at 1.18.59 PM

Class Methods

Class methods that are called directly on the class itself, without instantiating any objects.

Screen Shot 2016-06-20 at 1.27.15 PM

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.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s