In article <20020311202548.66307.qmail / web14304.mail.yahoo.com>, Damien Joly
wrote:
> Last week I posted a question about using Ruby in
> population ecology simulation, and I am very grateful
> for the replies I got.  It seems Ruby is exactly what
> I am looking for.
> 
> Of course, I have my first question/conundrum, and I
> hope it isn't too painful for experienced Ruby/OO
> programmers.
> 
> I am trying to simulate "death" in an array of objects
> ("Individuals") by tossing a "coin".  However, for
> some reason, I get results I am not expecting.  Here
> is my code:

[...]

> population = Array.new(5,Individual.new(1,0))

This sets population to an array containing 5 references to the same Individual
- to see the effect change Infividual's to_s to 

def to_s
    "#{self.id} Alive: #{@alive} Pregnant: #{@mother} \n";
end

and you'll see that your population is really just 1 object.

One way to fix it is

population = []
5.times {
    population << Individual.new(1,0)
}

and then you'll have 5 individual Individuals in your population.

Hope this helps,

Mike

-- 
mike / stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike / starnix.com                   | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA