using a module to create an object


T

Thufir

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
 
Ad

Advertisements

F

Frederick Cheung

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
 
T

Thufir

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 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
 
F

Frederick Cheung

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.
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 :)
 
Ad

Advertisements

T

Thufir

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top