# Using a module to create an object

**URL:** <https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199>\
**Category:** Ruby\
**Created:** [December 26, 2007, 10:41am UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199 "2007-12-26T10:41:07Z")\
**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:** [December 26, 2007, 10:41am UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199/1 "2007-12-26T10:41:07Z")

</div>

The following runs, which is good. But, I get these “nil” outputs for  
each object, rather than even the ?hash code? or ?object id? for each  
object.

I get an error if I try to: save.creature from within the loop. I’m  
trying to use the module:

thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ ruby db\_crud\_create.rb  
– create\_table(:creatures)  
SQL (0.011644) CREATE TABLE creatures (“id” INTEGER PRIMARY KEY NOT  
NULL, “type” varchar(255) DEFAULT NULL, “life” integer DEFAULT NULL,  
“strength” integer DEFAULT NULL, “charisma” integer DEFAULT NULL,  
“weapon” integer DEFAULT NULL)  
-\> 0.1323s  
nil  
nil  
nil  
nil  
nil  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ tail db\_crud\_create.rb -n 5

5.times do |index|  
creature = Instantiate.randomCreature  
puts creature.inspect  
end  
thufir@arrakis ~/Desktop/dwemthys $  
thufir@arrakis ~/Desktop/dwemthys $ cat instantiate.rb  
require ‘dragon’

module Instantiate

def Instantiate.randomCreature()

```
    creatureType=Kernel.rand(0)

    case creatureType
    when 0
            Dragon.new
    when 1
            Dragon.new
    end #case

```

end #makeCreature

end # Instantiate  
thufir@arrakis ~/Desktop/dwemthys $

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:** [December 26, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199/2 "2007-12-26T12:51:41Z")

</div>

On 26 Dec 2007, at 09:40, Thufir wrote:

> ```
> creatureType=Kernel.rand(0)
> 
> ```

You known that this produces a floating point number between 0 & 1  
(i.e your case 0 & case 1 statements will just about never be executed?

Fre

---

<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:** [December 26, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199/3 "2007-12-26T20:13:27Z")

</div>

On Wed, 26 Dec 2007 20:50:55 +0900, Frederick C. wrote:

> You known that this produces a floating point number between 0 & 1 (i.e  
> your case 0 & case 1 statements will just about never be executed?

1.) Frederick, you’re always helping me out 🙂

2.) I didn’t realize that about floating points, I want either one or  
zero, of course.

3.) I use svn at [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/dwemthys/source), and I can go  
back a few revisions for an example where it seemed to work (prior to  
putting in the db stuff) and the number of iterations matched the number  
of objects instantiated.

4.) I’ll do a couple prints and so forth on this angle, I seem to  
recall  
a built in ruby method for converting float to int, too.

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:** [December 26, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199/4 "2007-12-26T20:36:17Z")

</div>

On 26 Dec 2007, at 19:12, Thufir wrote:

> 

rand(n) with n integer gives you random integers less than n

> 3.) I use svn at [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/dwemthys/source), and I  
> can go  
> back a few revisions for an example where it seemed to work (prior to  
> putting in the db stuff) and the number of iterations matched the  
> number  
> of objects instantiated.
> 
> 4.) I’ll do a couple prints and so forth on this angle, I seem to  
> recall  
> a built in ruby method for converting float to int, too.

to\_i 🙂

---

<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:** [December 27, 2007, 7:58am UTC](https://www.ruby-forum.com/t/using-a-module-to-create-an-object/124199/5 "2007-12-27T07:58:29Z")

</div>

On Thu, 27 Dec 2007 04:35:20 +0900, Frederick C. wrote:

> rand(n) with n integer gives you random integers less than n

Ok, it works. I would’ve sworn that the other way worked, but  
apparently  
it was the problem.

Thank you,

Thufir
