[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using a module to create an object

Thufir Hawat

12/26/2007 9:40:00 AM

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


4 Answers

Frederick Cheung

12/26/2007 11:51:00 AM

0


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


> case creatureType
> when 0
> Dragon.new
> when 1
> Dragon.new
> end #case
> end #makeCreature
>
>
> end # Instantiate
> thufir@arrakis ~/Desktop/dwemthys $
>
>
>
>
> thanks,
>
> Thufir
>
>


Thufir Hawat

12/26/2007 7:13:00 PM

0

On Wed, 26 Dec 2007 20:50:55 +0900, Frederick Cheung 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 http://code.google.com/p/dwemt..., 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


Frederick Cheung

12/26/2007 7:35:00 PM

0


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

> On Wed, 26 Dec 2007 20:50:55 +0900, Frederick Cheung 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.
>
rand(n) with n integer gives you random integers less than n

> 3.) I use svn at http://code.google.com/p/dwemt..., 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 :-)

Thufir Hawat

12/27/2007 6:58:00 AM

0

On Thu, 27 Dec 2007 04:35:20 +0900, Frederick Cheung 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