From: Damien Joly Date: 2002-03-12T05:25:51+09:00 Subject: painfully newbie question 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: #Start class Individual attr_writer :alive, :mother def initialize(alive = 1,mother = 0) @alive = alive @mother = mother end def to_s "Alive: #{@alive} Pregnant: #{@mother} \n" end def survival if rand<0.5 @alive = 0 else @alive = 1 end end end population = Array.new(5,Individual.new(1,0)) puts "Before random death trials: \n" puts population.to_s puts "During random death trials: \n" for i in 0..(population.length-1) population[i].survival puts population[i].to_s end puts "After random death trials: \n" puts population.to_s # End The output I get is: Before random death trials: Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 During random death trials: Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 After random death trials: Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 When I would expect to get: Before random death trials: Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 During random death trials: Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 After random death trials: Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 1 Pregnant: 0 Alive: 0 Pregnant: 0 Alive: 0 Pregnant: 0 The value in "Alive" seem to be copied from the last iteration into the array population once the for loop is complete. I hope this question isn't too inane for the list, but I am stumped and would appreciate any insight. Thank you in advance, Damien __________________________________________________ Do You Yahoo!? Try FREE Yahoo! Mail - the world's greatest free email! http://mail.yahoo.com/