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

**URL:** <https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980>\
**Category:** ruby-talk\
**Created:** [7 November 2007 04:44 UTC](https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980 "2007-11-07T04:44:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [7 November 2007 04:44 UTC](https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980/1 "2007-11-07T04:44:53Z")

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

C:\code\>

thanks,

Thufir

---

<div class="post-metadata">

**Author:** ![Jacob\_Basham1](https://avatars.discourse-cdn.com/v4/letter/j/4491bb/32.png) [@Jacob\_Basham1](https://rubytalk.org/u/Jacob_Basham1)\
**Post date:** [7 November 2007 07:07 UTC](https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980/2 "2007-11-07T07:07:54Z")

</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  
&nbsp;&nbsp;module Creatures  
&nbsp;&nbsp;&nbsp;&nbsp;class Dragon  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Dragon#initialize"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;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|  
&nbsp;&nbsp;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

> **···**
>
> On Nov 6, 2007, at 10:44 PM, Thufir wrote:
> 
> > 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\>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|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someCreatures[i]=Dragon.new #all dragons? what about  
> > TeethDeer's?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "someCreatures["  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print i  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "]:\n"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print someCreatures[i].toString  
> > end
> > 
> > C:\code\>
> > 
> > thanks,
> > 
> > Thufir

---

<div class="post-metadata">

**Author:** ![Thufir](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thufir](https://rubytalk.org/u/Thufir)\
**Post date:** [8 November 2007 00:11 UTC](https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980/3 "2007-11-08T00:11:22Z")

</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:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [8 November 2007 01:11 UTC](https://rubytalk.org/t/instantiate-random-subclasses-of-creature-dragon-teethdeer-etc/41980/4 "2007-11-08T01:11:50Z")

</div>

fyi, you might find this interesting

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

regards.

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

> **···**
>
> On Nov 7, 2007, at 5:11 PM, Thufir wrote:
> 
> > 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
> 
> --  
> it is not enough to be compassionate. you must act.  
> h.h. the 14th dalai lama
