I went to my first Ruby meetup a few days ago. It was great to be part of a community of very smart people all working towards a common goal of learning. My question to one of the programmers there was, "When would you need to use a class variable or a global variable when programming?" their answer confused me. It confused me because I had no idea how Object Oriented Programming (OOP) actually worked.
Over the last few days I’ve been working towards developing a fuller understanding of OOP. My goal today is to describe and illustrate that in the simplest terms I can.
What is Object Oriented Programming?
The definition as described in the Well-Grounded Rubyist is as follows;
“Everything you handle in Ruby is either an object or a construct that evaluates to an object, and every object is an instance of some class.” If you’re anything like me, that statement will probably just confuse you.
Almost everything in Ruby is an instance of the Object class. If you’ve ever watched StarTrek, the ultimate enemies are The Borg. Essentially all of their minds are shared from a common place - the Queen Borg. In this case, the object class is the Queen Borg. Whatever thing can interact with the Queen Borg will also be able to interact with the sub-class of Borg. That’s how OOP works. Object sits at the highest point and every class created beneath the Object class (which is everything) inherits all of the methods that can interact with the Object class. Let’s take a look at how this works.
Imagine you are a chef with many, many recipes. As a chef, you like to keep track of your recipes in an online application. In order to save recipes, you are going to need to be able to create and generate a new recipe. Therefore, we can say “Recipes” are our class. When we create a new recipe like “Dill Salmon” we need to make sure it is an instance or exists from the Recipe class.
Here’s how we would create the necessary code to create that recipe
class Recipe
def initialize(cuisine, protein, *ingredients)
return [cuisine, protein, *ingredients]
end
end
dill_salmon = Recipe.new()
dill_salmon = the object or instance that was created from the class recipe
Recipe = The class that we created.
new = a method that we are able to use that interacts with the Recipe class. This ‘new’ method is actually inherited from the Master “Object Class”. There are many of these methods that are inherited from Object Class, ‘new’ is just one of them.
Now that we’ve created a new instance of Recipe called dill_salmon, we can add some content to this by using our method called ‘food’. ‘Food’ interacts with the Recipe instance of dill_salmon.
dill_salmon.(“American”, “Salmon”, “Dill”, “Olive Oil”, “Red Wine Vinegar”, “Salt”, “Pepper”).
>> ["American", "Salmon", "Dill", “Olive Oil", “Red Wine Vinegar”, “Salt”, “Pepper]
We now have the dill_salmon recipe created. Now if we ever wanted to search by ingredients, cuisine or protein we would need to define new methods that would interact with the objects. By doing this we first need to make a few changes to the food method by creating instance variables of the attributes we passed for cuisine, protein & ingredients.
class Recipe < Object
def initialize(cuisine, protein, *ingredients)
@cuisine = cuisine
@protein = protein
@ingredients = *ingredients
return [ @cuisine, @protein, @ingredients ]
end
def cuisine
return(@cuisine)
end
def protein
@protein
end
def ingredients
@ingredients
end
def everything
return [@cuisine, protein, @ingredients]
end
end
We can now call our new methods on our instance object of Recipes.
dill_salmon.cuisine
=> “American”
dill_salmon.protein
=> “Salmon”
dill_salmon.ingredients
=> [["Dill", "Oil", "Vinegar"]] fdill_salmon.ingredients
=> ["American", "Salmon", ["Dill", "Oil", "Vinegar"]]
That's how our new object (or instance of the Recipe class) dill_salmon can interact with methods. Objects are the things in Ruby that you want to interact with and in OOP, almost everything happens to be an object (but not everything). For the purposes of this post, we'll stop there - as that will create a pretty good base to learn more about OOP from.