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.
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.
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
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.
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.
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:
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.
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.
There are three things that happened here:
- The method new was called on the class Cookie.
- The method new returned a newly created object.
- 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.
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.
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.
yummy_cookie.class returns us a class Cookie. Yay! It means that our instantiation went successfully.