From: Robert Klemme Date: 2008-10-17T01:04:36+09:00 Subject: Re: newbie adding to array question On 16.10.2008 17:36, Roger Reeks wrote: > @fibresults= [] > > def initialize > > > @iterations = 10 > > @previous = 1 > > @current = 1 > > @fibresults[0] =@previous > > @fibresults[1] =@current > > > end > > def next_seq > > x = @current + @previous > > @previous = @current > > @current = x > > end > > > > def display_all > > count = 2 > while count < @iteration > > next_seq > > count = count + 1 > @fibresults[count] = @current > puts @current > > end > > end Here's how you can make your life easier: you do not need @previous and @current. Instead you only need the array. Note that you can access elements from the end, i.e. @fibresults[-1] and @fibresults[-2] access the last and last but one elements. Another simplification: the while loop can be replaced by @iteration.times do |count| end or 2.upto @iteration - 1 do |count| end Kind regards robert