# Instantiating random sub-classes (Dragon, TeethDeer, etc)

**URL:** <https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554>\
**Category:** Ruby\
**Created:** [November 7, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554 "2007-11-07T22:20:39Z")\
**Posts on this page:** 5\
**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, 10:20pm UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554/1 "2007-11-07T22:20:39Z")

</div>

In Driver, I was considering creating a pseudo-random number between 0  
and 4, then using case:

case 0  
AssistantViceTentacleAndOmbudsman

case 1  
Dragon

case 2  
DwarvenAngel

case 3  
IntrepidDecomposedCyclist

case 4  
TeethDeer

so that the array of Creatures is filled with a variety of monsters  
which inherit from Creature. However, there’s gotta be another way.  
Perhaps ask Creature what its sub-classes are? Then randomly select  
from that list/array of monsters?

Here’s the code:

C:\code\>  
C:\code\>type Driver.rb  
require ‘ArrayOfCreatures’  
require ‘Dragon’ #Dragon inherits  
from Creature  
require ‘Creature’ #Subclasses  
include: Dragon, TeethDeer, etc

#numOfCreatures=Kernel.rand(3)

puts “\nquantity of creatures:”  
numOfCreatures = gets.chomp.to\_i

someCreatures = ArrayOfCreatures.new

0.upto(numOfCreatures) do |i|  
someCreatures[i]=Dragon.new #what about other  
types (children/sub-classes) of creature?  
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, 11:53pm UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554/2 "2007-11-07T23:53:34Z")

</div>

Thufir wrote:

> In Driver, I was considering creating a pseudo-random number between 0  
> and 4, then using case:
> 
> case 0  
> AssistantViceTentacleAndOmbudsman
> 
> case 1  
> Dragon
> 
> case 2  
> DwarvenAngel
> 
> case 3  
> IntrepidDecomposedCyclist
> 
> case 4  
> TeethDeer
> 
> so that the array of Creatures is filled with a variety of monsters  
> which inherit from Creature. However, there’s gotta be another way.  
> Perhaps ask Creature what its sub-classes are? Then randomly select  
> from that list/array of monsters?
> 
> Here’s the code:
> 
> C:\code\>  
> C:\code\>type Driver.rb  
> require ‘ArrayOfCreatures’  
> require ‘Dragon’ #Dragon inherits  
> from Creature  
> require ‘Creature’ #Subclasses  
> include: Dragon, TeethDeer, etc
> 
> #numOfCreatures=Kernel.rand(3)
> 
> puts “\nquantity of creatures:”  
> numOfCreatures = gets.chomp.to\_i
> 
> someCreatures = ArrayOfCreatures.new
> 
> 0.upto(numOfCreatures) do |i|  
> someCreatures[i]=Dragon.new #what about other  
> types (children/sub-classes) of creature?  
> print “\n”  
> print “someCreatures[”  
> print i  
> print “]:\n”  
> print someCreatures[i].toString  
> end
> 
> C:\code\>
> 
> thanks,
> 
> Thufir

There are two ways to get a list of subclasses. One is to use  
ObjectSpace#each\_object, which is kind of messy, and the other is to use  
the Creature.inherited hook, to build up the array.

Alternatively, and to add more flexibilty, you could add a creature  
weights hash. Eg something like:

class Creature  
WEIGHTS = {}  
end

class Dragon \< Creature  
WEIGHTS[self] = 0.5  
end

class SomeScaryGuy \< Creature  
WEIGHTS[self] = 0.25  
end

(Doesn’t matter if they don’t add to one)

Or make the weights be dependent on some difficulty setting…

---

<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, 4:03am UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554/3 "2007-11-08T04:03:27Z")

</div>

Charles L. wrote:

> Thufir wrote:
> 
> > In Driver, I was considering creating a pseudo-random number between 0  
> > and 4, then using case:
> > 
> > case 0  
> > AssistantViceTentacleAndOmbudsman
> > 
> > case 1  
> > Dragon
> > 
> > case 2  
> > DwarvenAngel
> > 
> > case 3  
> > IntrepidDecomposedCyclist
> > 
> > case 4  
> > TeethDeer
> > 
> > so that the array of Creatures is filled with a variety of monsters  
> > which inherit from Creature. However, there’s gotta be another way.  
> > Perhaps ask Creature what its sub-classes are? Then randomly select  
> > from that list/array of monsters?
> > 
> > Here’s the code:
> > 
> > C:\code\>  
> > C:\code\>type Driver.rb  
> > require ‘ArrayOfCreatures’  
> > require ‘Dragon’ #Dragon inherits  
> > from Creature  
> > require ‘Creature’ #Subclasses  
> > include: Dragon, TeethDeer, etc
> > 
> > #numOfCreatures=Kernel.rand(3)
> > 
> > puts “\nquantity of creatures:”  
> > numOfCreatures = gets.chomp.to\_i
> > 
> > someCreatures = ArrayOfCreatures.new
> > 
> > 0.upto(numOfCreatures) do |i|  
> > someCreatures[i]=Dragon.new #what about other  
> > types (children/sub-classes) of creature?  
> > print “\n”  
> > print “someCreatures[”  
> > print i  
> > print “]:\n”  
> > print someCreatures[i].toString  
> > end
> > 
> > C:\code\>
> > 
> > thanks,
> > 
> > Thufir
> 
> There are two ways to get a list of subclasses. One is to use  
> ObjectSpace#each\_object, which is kind of messy, and the other is to use  
> the Creature.inherited hook, to build up the array.

An example of the Class.inherited method:

class Creature  
def Creature.inherited(new\_subClass\_that\_was\_defined)  
puts new\_subClass\_that\_was\_defined  
end  
end

class Monster1 \< Creature  
end

class Monster2 \< Creature  
end

class Monster3 \< Creature  
end

–output:–  
Monster1  
Monster2  
Monster3

You can save those sub Classes in an array:

class Creature  
def Creature.inherited(new\_subClass\_that\_was\_defined)  
puts new\_subClass\_that\_was\_defined

```
if not defined?(@@subs)
  @@subs = []
end

@@subs << new_subClass_that_was_defined

```

end

def Creature.subs #can access @@subs using: Creature.subs  
@@subs  
end  
end

require ‘creaturefile’  
require ‘monsterfile’

p Creature.subs

---

<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, 5:06am UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554/4 "2007-11-08T05:06:16Z")

</div>

Thufir wrote:

> On Nov 7, 7:03 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> […]
> 
> > ```
> > @@subs << new_subClass_that_was_defined
> > 
> > ```
> > 
> > end
> > 
> > def Creature.subs #can access @@subs using: Creature.subs  
> > @@subs  
> > end  
> > end  
> > […]
> 
> Ah, thank you for fleshing it out, but is it ok that @@subs isn’t  
> declared?

In ruby, variables aren’t declared–not even in classes. Instead, a  
variable springs into existence when a value is assigned to the  
variable.

---

<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, 4:39am UTC](https://www.ruby-forum.com/t/instantiating-random-sub-classes-dragon-teethdeer-etc/119554/5 "2007-11-08T04:39:38Z")

</div>

On Nov 7, 7:03 pm, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[…]

> ```
> @@subs << new_subClass_that_was_defined
> 
> ```
> 
> end
> 
> def Creature.subs #can access @@subs using: Creature.subs  
> @@subs  
> end  
> end  
> […]

Ah, thank you for fleshing it out, but is it ok that @@subs isn’t  
declared?

I seem to have created multiple threads on this topic, oops. Thank  
you everyone for the input, many good ideas and an almost overwhelming  
amount of information to absorb 🙂

-Thufir
