I want to not just create Dragons, but randomly other creatures.
Perhaps from a hard coded list? I was thinking of using random
numbers and case statements, but surely there’s ruby-esque way to
specify:
puts “\nquantity of creatures:”
numOfCreatures = gets.chomp.to_i
someCreatures = ArrayOfCreatures.new #need to ensure that this array
can only hold Creatures
0.upto(numOfCreatures) do |i|
someCreatures[i]=Dragon.new #all dragons? what about
TeethDeer’s?
print “\n”
print “someCreatures[”
print i
print “]:\n”
print someCreatures[i].toString
end
Just the other week I taught myself how to make a plugin type
architecture using a similar manner, their might be a better way to do
this, but this works and it’s simple.
Wrap all of the plugin (creatures in your case) in the same namespace,
and put them in their own directory.
i.e.
module Namespace
module Creatures
class Dragon
def initialize()
puts “Dragon#initialize”
end
end
end
end
Then you may dynamically import the creatures into your program, and
make an array of them in a snap.
This gives you an array of Strings containing the names of all classes
in the Namespace::Creatures namespace in the creatures directory. You
can then initialize any of your creature objects using it’s name in
the Array.
Ooops, I have two threads on this topic in the ruby group. The other
thread is "
instantiate random subclasses of Creature (Dragon, TeethDeer, etc) ".
Sorry, I’m at work and must’ve gotten distracted, posted twice by
accident.
This was along the lines of the solution I had in mind, although
another suggestion was to look into the inherited hook (?) which I’m
googling.