instantiate random subclasses of Creature (Dragon, TeethDeer, etc)

T

Thufir

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:

types of creatures:

AssistantViceTentacleAndOmbudsman.rb
Dragon.rb
IntrepidDecomposedCyclist.rb
TeethDeer.rb

and then (pseudo-) randomly select one to instantiate? Symbols seem
promising but I'm not sure how to implement it.

C:\code>
C:\code>
C:\code>type Driver.rb
require 'ArrayOfCreatures'
require 'Dragon'
require 'Creature'

#numOfCreatures=Kernel.rand(3)

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=Dragon.new #all dragons? what about
TeethDeer's?
print "\n"
print "someCreatures["
print i
print "]:\n"
print someCreatures.toString
end


C:\code>



thanks,

Thufir
 
J

Jacob Basham

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.

Dir['creatures/*.rb'].each { |path|
require path
}
creatures = Namespace::Creatures.constants

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.

creature = Namespace::Creatures.const_get(creatures[0]).new
# Dragon#initialize

Now just add the random factor you desire

someCreature =
Namespace::Creature.const_get(creatures[rand(creatures.size)])


Have fun!
Jake
 
T

Thufir

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.

Thank you for the food for thought :)



-Thufir
 
A

ara.t.howard

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.

Thank you for the food for thought :)



-Thufir

fyi, you might find this interesting

http://drawohara.tumblr.com/post/18708641

regards.

a @ http://codeforpeople.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top