Skip to content

Instantly share code, notes, and snippets.

@dbackeus
Created December 11, 2012 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbackeus/4259375 to your computer and use it in GitHub Desktop.
Save dbackeus/4259375 to your computer and use it in GitHub Desktop.
require 'yaml'
class Monster
def self.bestiary
@bestiary ||= YAML.load_file("monsters.yml")
end
def self.random
new bestiary.keys.sample
end
attr_accessor :name,
:attack_text,
:max_hit_points,
:strength,
:speed,
:health
def initialize(name)
attributes = Monster.bestiary.fetch(name) {
raise ArgumentError.new("Name must be one of #{Monster.bestiary.keys.join(", ")}.")
}
attributes.each do |attribute, value|
public_send("#{attribute}=", value)
end
self.name = name
self.health = max_hit_points
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment