Tiny lessons from Object Oriented Ruby.
omar0425
Posted on November 15, 2022
My blog post will discuss a few things I learned as I was learning object oriented Ruby. I will demonstrate classes and instances in the picture below along with an explanation.
Classes and Instances
Lets imagine we owned a car shop and wanted to create an app with a large inventory of cars. We may need to create an app to store the data of many cars. Many different attributes of many different cars may be stored in the application, and this information may be accessed at different times.
The name of the class in the above image is car. A class is basically a manufacturer of objects in programming. Every time we create an instance such as honda
, mazda
, and ford
. Our class has produced objects which are known as instances in Ruby.
pry(main)> honda
Every time an instance is called upon a class, Ruby assigns those weird numbers shown above. The "weird numbers" are known as Ruby Object Notation.
[4] pry(main)> honda == ford
=> false
To prove that each instance is different I used a
"==" operator. The value returned is false.
Instance Methods
Lets go back to our car shop for a minute and imagine that we upgraded all of our cars with new horns that create an irritating "Honk!" sound. How would we get our honda to sound its horn? It is as simple as calling an instance method on one or each of our instances.
Example:
In the image above I created the the method honk. I then called it on the instance "honda" in my terminal. We see that "Honk!" appears on the terminal. The next line shows "nil" simply because nothing was returned after the puts statement of "Honk!"
Setters and Getters
Our car shop is now growing and we finally got a very high-end sports car in our shop. It is very important to make sure we have the right model of this sports car. This is because we wouldn't want any confusion during the sale of this costly new sports car. How would we go about adding this "model" attribute to this car. We can use setters and getters.
Example:
With setters and getters we can add attributes to our car like the amount of doors, horsepower, and amount of cylinders. The setter method allows us to set the model of the car as shown in the image above. We then use the getter method to be able to use the data we put into the model attribute.
Writing setter and getters involves a lot of work. Fortunately Ruby gives us built-in methods to do the same thing with a lot less typing.
We can accomplish this through attr_writer and attr_reader macros.
Example:
attr_writer substitutes the setter method while attr_reader substitutes the getter method.
We can take it a step further and become more abstract by using the attr_accessor macro.
attr_accessor : model
This macro gives us a setter and Getter all in one. No need to repeat ourselves.
Conclusion
Learning Ruby at the Flatiron School has been a very positive experience for me. I find Ruby to be a very simple and clean language. I can make a class method for many objects we use day to day such as recipes, clothing, hotels, books, etc. We can then call instances of the class methods and create attributes for the instance that can be used many times over.
Posted on November 15, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.