# Instantiate random subclasses of Creature (Dragon, TeethDeer, etc)

**URL:** <https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462>\
**Category:** Ruby\
**Created:** [November 7, 2007, 5:45am UTC](https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462 "2007-11-07T05:45:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 7, 2007, 5:45am UTC](https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462/1 "2007-11-07T05:45:47Z")

</div>

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

C:\code\>

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 7, 2007, 8:18am UTC](https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462/2 "2007-11-07T08:18:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 8, 2007, 1:12am UTC](https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462/3 "2007-11-08T01:12:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [November 8, 2007, 2:12am UTC](https://www.ruby-forum.com/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/119462/4 "2007-11-08T02:12:58Z")

</div>

On Nov 7, 2007, at 5:11 PM, Thufir wrote:

> Thank you for the food for thought 🙂
> 
> -Thufir

fyi, you might find this interesting

[http://drawohara.tumblr.com/post/18708641](http://drawohara.tumblr.com/post/18708641)

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
