Superhero Aliases

lola odelola
blackgirl.tech: A Blog
2 min readJul 20, 2016

Superheroes meet ruby methods and all of a sudden it starts to make a little bit more sense.

The Problem

As an immigrant in Wakanda, you live a good life. Your king, T’Challa, is a good king. He’s going through some things at the moment, and the country is in a little bit of a state but you have faith in him. If you were to describe him, you’d probably describe him as:

One day you’re at work when there’s an attack on your office, Dr Doom has weaponised all the laptops to fight back. Your Mac is trying to eat you. Then out of nowhere, this cat like man pounces on your laptop, disconnects the hardrive and uses that to fight against the army of Macbook Pros. You pass out but when you come to, this cat-man has sorted everything out & has you in his arms, he introduces himself as Black Panther. When asked, you define this man as:

Later that night, you’re watching the news and you discover that T’Challa and Black Panther are the same person. This blows your mind, obviously, you begin to look at your definitions and think to yourself, how do I redefine `.tchalla` without overriding the functionality I already have.

The Fix

This is where an alias would be super useful. An alias, in ruby, allows us to redefine the functionality of a method while also keeping the original functionality, so essentially we get two methods for the price of one. There are two ways to alias in ruby, I’ll be speaking about the more common `alias_method`. It takes two arguments, the `:new_name` and the `:old_name`. So let’s start our definitions again.

Our `.tchalla` defininition needs to be changed to include the Black Panther alias, by using `alias_method` it would look something like this:

The `alias_method` sits outside of your method definition, first taking the new name you want to give the modified method and then the name of the original method. The redefinition sits under the `alias_method`. So now, when you call each method you get:

Now calling `.tchalla` gives us the method with the new additions, while calling `.black_panther` gives us the old method. Because the original functionality is maintained, all that you need to do is ensure the right method name is being called in the right place.

The End

In the real world you’re probably not going to be dealing with superheroes, and this is a simple example but aliasing is particularly useful in large, complicated apps where creating a new method is dangerous in that data can get overwritten or lost. It’s also best practice to only use aliasing for minor changes to methods, if the functionality of your method is completely different to the original, you may just want to create a new method.

In my talk I will explore the differences between aliasing styles and techniques, the technical cost of using either and best practices. Come down to the Ruby Tech Talk Night, hosted by Women Who Code London on August 2nd for more. blackgirl.tech are also fundraising, you can donate here.

--

--

lola odelola
blackgirl.tech: A Blog

@blackgirltech’s mum, published poet, coder, wanderer, wonderer & anti-cheesecake activist.