Using a module to create an object

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:

[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $
[email protected] ~/Desktop/dwemthys $ tail db_crud_create.rb -n 5

5.times do |index|
creature = Instantiate.randomCreature
puts creature.inspect
end
[email protected] ~/Desktop/dwemthys $
[email protected] ~/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
[email protected] ~/Desktop/dwemthys $

thanks,

Thufir

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

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 :slight_smile:

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

3.) I use svn at 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

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 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 :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs