# Creature module for the Dragon class

**URL:** <https://www.ruby-forum.com/t/creature-module-for-the-dragon-class/119681>\
**Category:** Ruby\
**Created:** [November 9, 2007, 6:17am UTC](https://www.ruby-forum.com/t/creature-module-for-the-dragon-class/119681 "2007-11-09T06:17:19Z")\
**Posts on this page:** 3\
**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 9, 2007, 6:17am UTC](https://www.ruby-forum.com/t/creature-module-for-the-dragon-class/119681/1 "2007-11-09T06:17:19Z")

</div>

I could have “instance” variables in Creature, which Dragon objects  
can use? Dragon would have all the methods of the Creature module  
available, for instance attribute accessors?

C:\code\>  
C:\code\>  
C:\code\>type Creature.rb  
module Creature

def sayHi  
print “\nhi\n\n”  
end

end  
C:\code\>  
C:\code\>type Dragon.rb  
require ‘Creature’

class Dragon  
extend Creature  
include Creature

def toString  
print “\ndragon\n”  
end

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 9, 2007, 7:32am UTC](https://www.ruby-forum.com/t/creature-module-for-the-dragon-class/119681/2 "2007-11-09T07:32:17Z")

</div>

Thufir wrote:

> I could have “instance” variables in Creature, which Dragon objects  
> can use? Dragon would have all the methods of the Creature module  
> available, for instance attribute accessors?

module Creature  
def kingdom=(some\_kingdom)  
@kingdom = some\_kingdom  
end

def kingdom  
@kingdom  
end  
end

class Dragon  
include Creature  
end

d = Dragon.new  
d.kingdom = ‘Mordor’  
puts d.kingdom

–output:–  
Mordor

---

<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 10, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/creature-module-for-the-dragon-class/119681/3 "2007-11-10T23:56:02Z")

</div>

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

> class Dragon  
> include Creature  
> end
> 
> d = Dragon.new  
> d.kingdom = ‘Mordor’  
> puts d.kingdom
> 
> –output:–  
> Mordor

This is interesting because it brings up the next problem:  
locations. In this case, kingdoms. I changed the design a bit so  
that Creature is a class from which Dragon and so forth inherit. One  
approach I was thinking about would be to give each creature instance  
a few objects, such as: statistics, kingdom and inventory. (Right  
now the “statistics” portion is built in directly to the class, I’m  
considering making a class for these traits.)

Each Creature needs a kingdom (even if it’s “banished” or something),  
and, of course, a kingdom can “hold” many creature objects. In  
tables, the “kingdom” table would have a one-to-many relation to the  
“creatures” table.

Just thinking outloud…

-Thufir
