The Dungeon Master’s Guide to Coding: Ruby Class Inheritance

“A monster is defined as any creature that can be interacted with and potentially fought and killed. Even something as harmless as a frog or as benevolent as a unicorn is a monster by definition.”

— Monster Manual, Dungeon and Dragons Fifth Edition

What does this spell mean by ‘creature’? I asked this question often when I began playing Dungeons and Dragons. My adventures would grind to a halt as I poured over the core rulebooks looking for an answer. Eventually I learned and internalized that D&D Fifth Edition(5e) classifies anything with a stat block as a creature.

The undead Lich locked up in that tall tower, the dragon resting deep within a dead volcano, the kind tavern keeper, even your character.

All of them are considered a creature.

Learning about class inheritance in Ruby gave me a lovely way to visualize this idea:

Side note: I was told that a side effect of getting into coding was breaking down your everyday life into code concepts and snippets. Antonio. You were right.

Let’s say we’re writing a Creature class in Ruby. We want all instances of a creature to have a stat block, or a list of attributes, and a name. Our class would like something like this.

Image for post

Since everything with a stat block is a creature, let’s make a dragon.

Because dragons are cool as hell.

Image for post
source: Wizards of the Coast
red_dragon ="Adult Red Dragon", 256, 27, 10, 16, 21)=> #<Creature:0x00007fc3ac2425f8 @cha=21, @dex=10, @hp=256, @int=16, @name="Adult Red Dragon", @str=27> 

How about we give our instances a method that allows them to tell us what kind of class they are? Self-awareness is pretty sweet, after all.

Image for post

would read:

=> "I'm a Creature!"

But wouldn’t we want a dragon to be more specific than that? Wouldn’t we want a dragon to be a dragon and a creature?? What would be the easiest way to achieve that?


Through class inheritance!

Let’s add a new class, and this time call it Dragon. Our Dragon class is the “child” to our Creature class, and will inherit the attributes and methods contained in the Creature class.

Image for post

Notice this snippet of code

class Dragon < Creature

The “<” is our heavy lifter here. With that little character, our Dragon class inherits all of the functionality of our Creature class.

So let’s create an instance of a Dragon class.

red_dragon ="Adult Red Dragon", 256, 27, 10, 16, 21)
=> #<Dragon:0x00007fa008192850 @cha=23, @dex=24, @hp=275, @int=18, @name="Adult Red Dragon", @str=18>

Check that out! We can give an instance of the Dragon class the exact same parameters our Creature class requires, but when we as our “red_dragon” what class it is we’ll get back the following:

=> "I'm a Dragon!"

This is extremely significant for building out functionality. We can even give our Dragon class attributes and methods that are exclusive to it.

Image for post

Every dragon needs a deadly breath attack, right? So let’s give our red_dragon a breath_type like this:

red_dragon.breath_type = "fire"
=> "fire"

Now let’s call our breath_attack function.

=> "The Adult Red Dragon exhales a fury of fire."

With class inheritance, we can make similar children classes for undead creatures, humanoids, demons, YOU NAME IT! The sky (or the multiverse) is the limit!

And because of how both our code and our rulebooks have defined all creatures, you never have to wonder if your Hypnotic Pattern spell effects are applicable to your target. They definitely are ;)

Alex Mata is a software developer located in Houston, Texas. He plays D&D and listens to vaporwave whenever he isn’t solving problems through coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store