From: Damien Joly Date: 2002-03-12T06:34:25+09:00 Subject: Re: painfully newbie question Thanks to everyone for replying so quickly. I thought I'd post the consensus answer so that other newbies might be able to avoid my mistake. It seems that what I've done is this: population[0]---\__ population[1]--\ \ population[2] -----> Invdividual.new() population[3]--/ __/ population[4]---/ When this should work: population = [] 5.times { population << Individual.new(1,0) } Alternatively, I could overload "array" using: class Array alias_method :old_init, :initialize def initialize(num = 0, val = nil, &proc) if block_given? num.times {|i| self.push (yield i)} else old_init(num, val) end end end In order to allow: population = Array.new(5) {Individual.new(1,0)} Thanks again, Damien --- 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: > > #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/ __________________________________________________ Do You Yahoo!? Try FREE Yahoo! Mail - the world's greatest free email! http://mail.yahoo.com/