Pallet Town: Ruby Modules, Mixins & Flying Pikachu?

Mar 9, 2025 | Pallet Town, Programming, Ruby

If you’ve been working with Ruby for more than five minutes, you’ve probably heard about modules. They’re a great way to share behavior across multiple classes without getting tangled in inheritance chains that resemble an over-leveled Zubat spamming Confuse Ray.

But let’s make it real. Instead of talking about include and extend in the abstract, we’re going to build something solid—Pokémon abilities using modules. Let’s make sure that pesky Pikachu keeps his paws on the ground!

The Problem: Not Every Pokémon Can Fly

Let’s say we’re designing a simple Ruby class for our Pokémon. We might start with something like this:

class Pokemon
  attr_reader :name, :type

  def initialize(name, type)
    @name = name
    @type = type
  end

  def attack
    puts "#{@name} uses a basic attack!"
  end
end

pikachu = Pokemon.new("Pikachu", "Electric")
pikachu.attack # Pikachu uses a basic attack!

Cool, but here’s the issue: some Pokémon can fly, some can swim, and some can dig underground. These aren’t things that should be in every Pokémon’s base class because that would make zero sense—imagine a Snorlax trying to fly.

Modules, I Choose You!

Instead of bloating our Pokemon class with abilities that don’t apply to every Pokémon, we break them out into modules:

module Flyable
  def fly
    puts "#{@name} soars through the sky!"
  end
end

module Swimmable
  def swim
    puts "#{@name} dives into the water!"
  end
end

Now, let’s apply these abilities where they make sense.

class FlyingPokemon < Pokemon
  include Flyable
end

class WaterPokemon < Pokemon
  include Swimmable
end

Now to try it out!

pidgeot = FlyingPokemon.new("Pidgeot", "Flying")
pidgeot.fly # Pidgeot soars through the sky!

gyarados = WaterPokemon.new("Gyarados", "Water/Flying")
gyarados.swim # Gyarados dives into the water!

 

Why This is Better Than Inheritance Alone

You might be thinking, “Wait, why not just have a FlyingPokemon class that inherits from Pokemon and adds a fly method?”

Good question. The answer is that Pokémon can have multiple abilities that aren’t strictly tied to their typing. Take Gyarados—it’s a Water/Flying Pokémon, but it can swim and fly. Instead of creating an ugly WaterFlyingPokemon class, we just mix in both modules:

class Gyarados < Pokemon
  include Flyable
  include Swimmable
end

gyarados = Gyarados.new("Gyarados", "Water/Flying")
gyarados.fly  # Gyarados soars through the sky!
gyarados.swim # Gyarados dives into the water!

This keeps our code modular, reusable, and avoids deep inheritance hierarchies that turn into spaghetti faster than a poorly planned Rails app.

Final Thoughts

Modules are a great way to share behavior across classes that aren’t directly related. It’s the same reason we don’t shove everything into one Pokémon class—forcing Diglett and Charizard to follow the same move rules would just break the game. Use modules to separate concerns, keep code clean, and most importantly, make sure Pikachu never learns Fly. If you enjoyed this, have a look at Coder Radio and if you help automating data-driven tasks in your organization, then checkout Alice.

Don’t forget to checkout my Earth Day challenge if you are a US middle or high school student interested in coding and possibly helping the environment, checkout the details here. PS the grand prize is a brand new System 76 Thelio!

More from Mike:

About Me

Hi! I’m Mike! I’m a software engineer who codes at The Mad Botter INC. You might know me from Coder Radio or The Mike Dominick Show.  Drop me a line if you’re interested in having some custom mobile or web development done.

Follow Me

© 2025 Copyright Michael Dominick | All rights reserved