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.Â