self-ish ruby

Connor Mulholland
5 min readNov 9, 2020

Welcome to the world of Object-Oriented Ruby! Object-Oriented Ruby may seem complicated and overwhelming now, but fear not for I am here to show you that it can all be quite simple. Storing your codes in classes will make your program more efficient and readable than ever before. Ruby is a sugar-sweet language that is jam-packed full of helpful tools to help us make our code run at maximum efficiency. Ruby is littered with great macros that help us accomplish a myriad of tasks. Ruby is designed with you, the programmer in mind and it wants you to be as lazy as can be, so utilizing these tools that it provides can maximize both our power and our laziness!

Photo by Chris Ried on Unsplash

ATTR MACROS

Ah, our attr-macros and how easy and beautiful they make our code! Once you dabble with an attr-macro, there is no going back! Gone are the days of writing out your getter and setter methods. Taking up precious space and wasting your valuable time! We can place an attr_writer near the top of our construction of our class to build a setter method, an attr_reader to create a getter method or we can use an attr_accessor to build both of those for us. These simple macros pack a powerful punch and they make our code way more versatile with just one simple line. One to two lines of code and our class is already half built! Love that for us! Below is an example of how each might look for some info we’d like to initialize for a class of Dog.

Dog Class with attr-macros

Storing ALL the class info

You might be thinking, sure, this is great that we’ve created all this. Sure we’ve built a class that can create new instances of Dogs (or whatever you prefer, some people prefer cats, I’m versatile). But where will we store all this information so we can access it? You’re totally right! We need a method to access all of the info that this class is storing. Thats where a handy class variable comes in! Class Variables begin with @@ and a typical convention for classes is to store all the information inside of a class variable named @@all set equal to an empty array(for now), I mean, right? Makes total sense that what we’re storing ALL of the info in is named @@all. But we also need a method to access ALL of that information stored in @@all. We could build a method Dog.all that acts as a reader for the array but another Ruby convention makes our code more dynamic and versatile for future use.

Access those variables

Variables look a bit different here in OO Ruby. You can see above that there are all of these @’s throughout our code and its not by accident and it certainly isn’t Twitter. One @ creates an instance variable which differs from other variables in the sense that it becomes available to the entire instance class instance. This makes our code way easier to work with instead of creating and altering tons of local variables throughout our code. A variable with two @@’s creates a class variable which is accessible throughout the entire class as well but resembles closer to a global variable. Its main use is to store the information we create about our class inside of an array. The @@all we discussed earlier.

Lazy SELF-ish Programmers

If I have learned one thing since starting Flatiron School, it is that programmers are very lazy. It has been crammed into my head almost more than anything else I’ve learned and I have accepted this mantra because I also am quite lazy. Ruby knows this and provides us tools to allow for further laziness. self is a critical and useful tool in Ruby that not only allows for code versatility but allows for laziness as well! Yaaas self! self is used to represent the instance of the class that is being called on. Above where we have written def Dog.all, could be replaced with def self.all instead. This can easily be copied and pasted into other code if needed and will still work with almost no changes as well as preventing us from having to alter numerous lines of code that may have been named Dog.average_age, Dog.all_names, etc. If we changed the name of the class, we could have to rename all of these methods and I don’t know if I mentioned this yet but ... programmers are lazy! We can replace all these instances of Dog. with self. and get the same result with far more versatility.

Using all of these tools together makes our classes more powerful and versatile (I keep saying that but it really is true!). These make us better and more efficient programmers but keeping out code clean and lean! Usage of all of these tools will take your code to the next level and make other programmers look at your code and truly admire the ability outwardly displayed through your code.

These are just a few of the tools that are included in Ruby that make our lives as programmers, simpler. They are there to make our code more elegant and less repetitious. Believe it or not, there are even more tools that we can use to make our code EVEN simpler and more versatile. That is kind of hard to believe but it’s true but for now these tools will transform your code and make it way more powerful both to you and to a user. For now, the code that code you’re crafting will be the most beautiful, efficient and elegant code to you, until you learn how to be lazier, which you will. But even when you do get lazy don’t forget about Ruby and it’s “magic” conventions that make it so simple and powerful, all at once.

--

--