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