Coding as Sorcery: OOP and the Magic of the True Name

Alec Magnet
CodeX
Published in
7 min readAug 24, 2021

Software coding is a lot like sorcery. You string together arcane and powerful words and symbols, and then the world — or at least your webapp — changes. New elements appear or disappear on the page. Forms and inputs change both what’s displayed and what’s in your in your database.

When you first start learning, your code feels a lot like Harry Potter magic — just some abracadabra-sounding gobbledygook that makes things happen for unspecified reasons (probably because it looks like Latin).

As you get deeper into coding, though, you start to understand — little by little — a bit more about how and why the magic works the way it does.

Someday, I’d like to do a deeper dive into how various programming languages compare to the magic systems of different fantasy properties. I’ve just been learning Ruby in my bootcamp, though, so today I’m going to home in on one particular comparison — object-oriented programming and the concept of the true name.

What Is OOP?

Object-orientation is a way of abstracting your data so that each example groups together not only the content of the data, but also a list of things you can do it — the operations you can perform with it, the other data it is associated with, and so on.

In Ruby, each object is an instance of a Class, and each class defines a set of methods you can call on each instance, as well as a set of relationships each method may have.

For example, you might define a class of Broom and create a method called fly:

class Broom 
def fly
puts "Up! This broom is flying!"
end
end
$ nimbus_2000 = Broom.new
$ nimbus_2000
#=> #<Broom:0x007fc52c2cc588>
$ nimbus_2000.fly
#=> "Up! This broom is flying!"

Calling .new on the class Broom instantiates a new object, nimbus_2000, a new instance of the class of Broom. This new instance has data about it — we know, at least, where it is being stored in memory (which is what that string of letters and numbers our terminal returns tells us). We also have something we can do with our broom — call #fly on it.

The Name of Every Thing

In this way, OOP is less like Harry Potter magic, with its magic words, than it is like at least one of the types of magic in The Earthsea Cycle by Ursula K. Le Guin or in The Kingkiller Chronicle by Patrick Rothfuss. In these stories, if a character knows the true name of the wind, they can summon, transform, and control it, just as knowing that nimbus_2000 is an instance of Broom lets us know that we can call .fly on it.

Cover of The Books of Earthsea: The Complete Illustrated Edition by Charles Vess showing a man and woman walking through water they control by magic while a massive red dragon flies past them.
Ged and Tenar from Earthsea illustrated by Charles Vess

True-name magic has a long history not only in fiction, but also in mythology and religion. Think, for example, of the power granted over Rumpelstiltskin in Grimm’s Tales by knowing his name. Think, too, of the Jewish prohibition on erasing or defacing the written name of God.

In fact, it goes back much further. There’s an ancient Egyptian narrative from the time of the Middle Kingdom in which Isis tricks Ra into revealing his secret name to her — nothing else is powerful enough to neutralize the venom with which she has poisoned him. Ra is so concerned about the power that Isis will have over him once she knows his true name that he only agrees to be saved this way if she will promise to tell no one but her son Horus — and only if Horus vows secrecy himself.

Ptah receiving homage from the 19th-dynasty tomb of Irynefer
Ptah receiving homage, from the 19th-dynasty tomb of Irynefer

In fact, in the Memphite version of the Egyption creation story, the god Ptah brought the world into existence when he, according to the Shabaka Stone, “pronounced the name of every thing”—including the other gods, their shrines, the cities of Egypt, food, and “all good things.”

A Class Is an Instance of a Class

I hope no one will think I’m being glib about an ancient culture’s mythology when I say that this story provides an excellent analogy to help understand the OOP concept of class inheritance.

Let’s imagine Ptah creating a new, delicious food — let’s say this piece of fruit or bread:

Ancient Egyptian wall-painting of a lady offering another lady a lemon
Detail of a wall-painting in the tomb of Nakht (16th-14th BCE), in the cemetery of Sheikh Abd al-Qurnah

Let’s say it’s a fruit. (UPDATE: it’s bread.) The analogy for this creation in Ruby is instantiation. In OOP terms, this particular piece of fruit may be thought of as an instance of whatever type of fruit it is (a very faded pomegranate? a fig??):

$ this_particular_fruit = Pomegranate.new
$ this_particular_fruit
#=> #<Pomegranate:0x007fc52c2d7d20>

Pomegranate is the class, while this_particular_fruit is the object — an instance of this class. However, Pomegranate is itself an instance of larger class, Fruit in general, and it inherits some of its parent class’s properties. It’s organic, has seeds, and is part of the reproductive cycle of the tree or plant it grew on.

In Ruby, we might represent this relationship as follows:

class Fruit
def seeds
"I have seeds!"
end
end
class Pomegranate < Fruit
def seeds
super
"My seeds are edible and delicious!"
end
end
$ that_fruit_in_the_picture = Pomegranate.new
$ that_fruit_in_the_picture.seeds
#=> "I have seeds!"
"My seeds are edible and delicious!"

The < syntax establishes that Pomegranate is a type of Fruit and thus inherits its methods. The keyword super allows us to build on a method by including everything that method does in the superclass, then adding new functionality in the subclass — hence why that_fruit_in_the_picture.seeds returns both “I have seeds!” and “My seeds are edible and delicious!”

Lemon from Wikimedia. Bread from X-Box co-creator Seamus Blackley, who baked it with traditional tools and leavening cultures sampled from ancient Egyptian baking vessels.

UPDATE: I don’t know why I saw a pomegranate. If it is a fruit, it’s obviously a lemon. But it’s not. It’s bread. If it were a lemon, though, you would expect to see a different return if you called .seeds on it:

class Lemon < Fruit
def seeds
super
"My seeds are not the part of me that's good to eat!"
end
end
$ actually_a_lemon = Lemon.new
$ actually_a_lemon.seeds
#=> "I have seeds!"
"My seeds are not the part of me that's good to eat!"

Fruit itself might be an instance of Food or Plant, which in turn may be an instance of LivingThing, and so on all the way up to Everything.

The same is true in Ruby. Just as, in another religious tradition, it’s turtles all the way down, in Ruby, it’s objects all the way up to the main object. This is the top-level scope of a Ruby program. Methods defined outside of another Class or Module belong to this object, which Ruby calls “main.”

The cover of The Earthsea Trilogy depicting the characters Ged and Tenar and a dragon

Messing with the Fabric of Reality

Because in Ruby it’s objects all the way up, you can mess with the fabric of reality within your Ruby app. You can define new methods — and even hijack existing methods — that belong to the core classes built into Ruby itself. This form of hubris is called monkey patching, and it can completely break your code — or, apparently, come in quite handy.

All you need to do to hijack a method that belongs to a core class is redefine that method within the class:

class String
def upcase
self.reverse
end
end

Now, when you call #upcase on an instance of string, instead of capitalizing all the letters, your program will return them in reverse order:

$ 'hello world'.upcase
# => "dlrow olleh"

In this way, Ruby may be closer to the magic of true names in Le Guin’s Earthsea books than in Rothfuss’s Kingkiller. (It’s hard to know for sure, since Rothfuss seems to be pulling a George R. R. Martin, and who knows if we’ll ever see the last volume of his trilogy.) But Rothfuss’s Kvoth seems like he can only call down the wind with its true name. Le Guin’s characters, by contrast, must be thoughtful and sparing in how they use the true words for things to manipulate their world, since every use of magic changes and reorders things at their root. Just as monkey patching can break everything, so too can the wizards of Earthsea.

Art from Wizards of the Coast

A Conference of the Mages

Please tell me what you think! I’d love to hear more ideas on the overlaps and analogies between programming and various magic systems or mythological beliefs. Write me a response on Medium or reach out through LinkedIn to keep this conversation rolling.

--

--

Alec Magnet
CodeX
Writer for

Fullstack software engineer, recovering English professor, enthusiast