Day 6: Classes, Methods and Objects

Classes

You’ve made it to this point, congratulations! We’ve covered a lot of basics. Now we’re approaching Classes and Objects. It’s a cornerstone of the most common technique called Object Oriented Programming.

Ruby class is like a blueprint Photo by fdecomite

Here is what it is. Class is a blueprint. Individual objects or instances are created from the blueprint. See those yummy cookies at the picture? Class is the cookie cutter. Individual cookies are objects. As long as you have one cookie cutter, you can make as many cookies as you like. They all will be the same.

Let’s write our simple Cookie class.

class Cookie
end

That’s it. Class declaration starts with the keyword class followed by a class name. The keyword end finishes the class declaration.

The convention for class names is what is called camel case. First character of a word is upper case. Following characters are lowercase. Examples: CookieCutter, SomeOtherClassName, etc

Now we have our Cookie class, we can’t wait to create some instances of Cookie. In order to do it, we need to learn a little more about methods.

Methods aka Functions

Mixer Photo by g2boojum

Method (aka function) is a unit of code that receives one or many parameters. It does some work and returns the result. Imagine a mixer. You load the ingredients into a mixer. It mixes it up. You have your cookie dough in return.

def mix(butter, sugar, eggs, flour)
  puts "Mixing ingredients"
  final_mass = butter + sugar + eggs + flour
  return final_mass
end

A method definition starts with the keyword def. Then you specify input parameters surrounded by a pair of parenthesis. The code in between the keywords def and end is called a function body. It usually does some useful work with input parameters. Given that each input parameter is an integer representing mass of each ingredient. The body of mix function simply adds up the ingredients. Now we have final_mass variable. Let’s return it using the keyword return.

A method definition does not actually run any code. We need to call a method in order to use it.

mix 1, 1, 2, 3

Try it out in your IRB. You’ll need to define the function first. Then you can call it. You should get the return value of 8. Here is the terminal output:

$ irb
>> def mix(butter, sugar, eggs, flour)
>>   puts "Mixing ingredients"
>>   final_mass = butter + sugar + eggs + flour
>>   return final_mass
>>   end
>> :mix
>> mix 1, 1, 2, 3
Mixing ingredients
>> 7

As you might’ve already noticed, puts is actually a method provided by Ruby by default. It takes a string of text as an input parameter and prints it out on a terminal.

Objects

So far, we’ve defined methods outside of any class. Methods defined outside of any class are called global methods. They are available anywhere in our program.

Methods can also be defined inside classes. If a method is defined inside a class, it can only be called on that class using dot notation. Every class has a number of builtin methods. One of those methods is called new. It’s very useful. Every single class has it by default. Let’s see how we can use it to create a new Cookie.

yummy_cookie = Cookie.new

There are three things that happened here:

  1. The method new was called on the class Cookie.
  2. The method new returned a newly created object.
  3. The newly created object has been assigned to yummy_cookie variable.

Let’s talk about each of the things.

First, dot notation. Dot notation is a way to write code by specifying a class name, followed by a dot and a function name.

Dot notation in Ruby

Second. The method new returns a newly created instance. You can use the method new as many times as you want. Every time you call it, it’s going to return a new, different object of the same class.

Third. We’ve assigned the newly created object to yummy_cookie variable. From now on, we can refer to the object using this variable. How do we know if it’s an instance of the class Cookie? We’re going to call a method on it that returns the class of this instance. This method is called (guess how?) class.

yummy_cookie.class

Every method returns a value. The class method is no exception from this rule. It returns you a class of a variable. Try it out in IRB.

$ irb
>> class Cookie
>>   end
>> nil
>> yummy_cookie = Cookie.new
>> <Cookie:0x007fb0e1024f38>
>> yummy_cookie.class
>> Cookie

yummy_cookie.class returns us a class Cookie. Yay! It means that our instantiation went successfully.