OOP , Mark III— Ruby Cheat Coding

Beyond inheritance, another concept you will run into while learning object oriented programming are helper methods — or, as I called it when I started, that weird stuff I was told to put at the top of my classes. They’re not well explained, but once you understand what’s happening, they’re very important, very useful shortcuts to writing ruby classes.

Most of the online tutorials I ran through had me put either or both of those in each class I built, but almost none of them explained why. And when they did, it was some kind of vague “you just have to, or you won’t be able to access stuff in your class objects.”

And without these methods, I couldn’t do otherwise simple tasks like check how many wheels my car had, or adjust speed once I created my vehicle, so I just went with it, and whenever I ran into a weird “undefined method” error around my classes, I’d just add attr_reader, writer or accessor, and see if it made a difference.

Under the Hood

After some exploring, though, I was able to figure out what exactly these helper methods were doing: by default, ruby does not allow you to access an object’s variables from outside the object — in order to keep its data protected from outside bugs/tampering. You can still allow your program to view or change a given variable for an object, but you have to explicitly write a method to allow it.

Here are some examples of very basic methods that allow you to either read a variable or overwrite it with some new value.

This explicit approach is good for restricting permissions and maintaining data integrity, but it’s very repetitive — if you had a class with a lot more instance variables, writing out these accessor methods would get really tedious really fast, especially since they are all of one of these two forms. So, ruby introduced three shortcuts that automatically write these simple methods for you:


Like its name suggests, attr_reader allows you to read an attribute for an object outside of the object itself. This is the same access lines 8–10 above were giving us for the @wheels constant. And in fact, when you write

attr_reader :wheels

in your class, ruby literally writes those three lines for you behind the scenes. The end result is the same class that we described before, but with less typing.


In the same way, attr_writer allows you to update an attribute of an object from outside the object. In the vehicle example, this is the equivalent of giving a driver the ability to hit the gas and change speeds. This is the same functionality outlined in lines 12–14, and like before,

attr_writer :speed

literally builds lines 12–14 on the backend of your program, without you having to write out the whole thing. So, you can replicate the exact same functionality from the class above by putting this instead:


It’s at this point where the ruby shortcuts get a little silly, but it’s worth knowing because you will see it: attr_accessor is an even further shortcut — it builds both an attr_reader and attr_writer method for the attribute you’re working with. So, if you want to be able to read or update an attribute from outside an object, you can write either

attr_reader :attribute
attr_writer :attribute

or just this in one line

attr_accessor :attribute
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.