Created
December 11, 2012 15:31
-
-
Save dbackeus/4259375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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