Harry Potter and the Classes of Ruby

I think by now it is obvious that I love Harry Potter. And much in same way that utilizing the art of Occlumency (closing one’s mind, emptying it of all thoughts) is helping me sleep easier whilst in the midst of programming madness, so is using a story that I am so familiar with helping me learn the tougher concepts of coding.

This post is meant for those who are new to programming in general or are a bit confused about the particulars of object oriented programming.


Ruby is an object oriented programming language. Which means everything in Ruby is an object (number, words, strings, etc). Classes are a key component of the Ruby language. A class is just a way to group things that are similar much like the classic biological classification system: Domain, Kingdom, Phylum, Class, Order, Family, Genus, and Species. There are classes that already exist in Ruby (like the Numbers class which consists of {you guessed it!} numbers!), and you can also make any class you want and you can define that class in any way you want. You can create any number of members (instances) that can belong to that class and you can make the class and its instances behave in any way you like. When learning Ruby, I found object orientation and the idea that different objects could interact and call on each other to be a bit confusing.

In order to make this clear (for any interested readers) I am going to make a few classes based on characters from the Harry Potter series and try to break down the concepts of object orientation, and how dynamic methods work.

First, I’m creating a WizardWitch class:

Each new Witch or Wizard I create will have to take a name and a wand (after all, the wand chooses the wizard):

Awesome! Now I can start making Wizards and Witches! So let me quickly make a few:

Cool! Now we have a way of making whatever Witches and Wizards we want! But that might get boring after awhile. So I am going to make a new class called Pet. As everyone knows, pets play an important part in the lives of witches and wizards (especially as students at Hogwarts). We will initialize each pet with a name, sound, and species:

Let me create a few pets!

And lets change the WizardsWitches class to include a pet (bc in this case I want to say a pet belongs to a wizard or a witch- in other words, I want the classes to be able to interact with one another):

Great! So how do we make Hedwig belong to Harry? Just like so!

And now whenever we call the .pet method on Harry, we will see that Hedwig belongs to him. But what if I wanted to do more than that? Let’s say that I wanted to let the wizard/witch be able to interact with their pet and have something happen. I’m going to make a method in the Wizard class that will let a wizard/witch play with their pet and will return the pet’s signature noise:

Now when I call the method stroke_pet on harry, I will expect to get Hedwig’s signature “HOOOOT” back:

Awesome! I am going to assign the rest of the pets to their rightful owners. But, wait! Remember when Ron’s rat turns out to be an evil wizard and he gets a new owl? We can update Ron’s pet like this:

And now Ron’s pet is permanently Pigwidgeon (unless I decide to change it again).

Hopefully this was a helpful introduction to creating objects and object interaction in Ruby. At first it can be a difficult concept to wrap one’s head around. When learning a new language (or new concept in general) it can be incredibly useful to conjure up examples using something familiar or something you love. For me, it’s the Harry Potter books, but anything can work (think music, movies, stories, sports).

Have fun coding!

Like what you read? Give Blythe C. a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.