why's (poignant) guide to ruby

Dwemthy's Array — Ruby Mini-Dungeon

by _why the lucky stiff  ·  CC BY-SA 2.5
Original DomainRuby ProgrammingClassic 2003–2009
Dwemthy's Array — the Ruby dungeon adventure

Dwemthy’s Array is a dungeon adventure game written in Ruby by _why the lucky stiff. It is a showcase of Ruby metaprogramming — particularly the use of method_missing, class macros, and DSL design. The game is short enough to read in one sitting but deep enough to teach real Ruby techniques.

The Creature Module

module Creature
  def self.included(base)
    base.instance_eval do
      def traits(*arr)
        arr.each do |trait|
          attr_accessor trait
          define_method(trait.to_s + '=') do |val|
            instance_variable_set('@' + trait.to_s, val)
          end
        end
      end
    end
  end
end

class Rabbit
  include Creature
  traits :life, :strength, :charisma, :weapon

  def initialize
    @life      = 10
    @strength  = 2
    @charisma  = 44
    @weapon    = 4
  end
end

The Array

class DwemthysArray < Array
  alias _push push

  def push(val)
    _push val
    self
  end

  def >>(val)
    push(val)
  end
end

Why It Matters

Dwemthy’s Array demonstrates that Ruby DSLs can look like entirely different languages. The traits macro, the >> operator for adding enemies to a dungeon array, the Creature module mixin — these patterns appear throughout production Ruby code. Rails uses similar approaches for has_many, validates, and other class-level macros.

The complete source and an extended version are archived in the whymirror GitHub organization.

Why's (Poignant) Guide to Ruby is released under the Creative Commons Attribution-ShareAlike 2.5 license. Written by _why the lucky stiff, originally published 2003–2009. Preserved at its original domain. Images © _why the lucky stiff, CC BY-SA 2.5.

→ AI Detector Benchmark & Research Hub