From: Tim Hunter Date: 2009-08-29T08:32:39+09:00 Subject: Re: Error with an array of strings Tom Smith wrote: > Thanks all for your help. > > Josh, that's definitely an improved loop, thanks. In any case, here's > the improved code I've ended up with (pre-changing the loop): > > all_inputs = Array.new > > until $_ == "n" > puts "Enter Number:\n" > response = gets > response.chop! > response.to_f > > all_inputs.push(response) > > print "add more numbers to average? y or n\n" > continue = gets > continue.chop! > > end > > averageall = all_inputs.inject(0){|sum,num| sum + num.to_f } / > all_inputs.length > > print "Average is: ", averageall,"\n" Note that response.to_f doesn't actually do anything. The to_f method returns a number, it doesn't change the value in response, so all you're doing it converting the string to a Float and then discarding it. Instead, what you should do is something like this: all_inputs.push(response.to_f) Since the array values are already floats, you won't have to convert the array values to numbers in the inject block. -- MagickWand for Ruby - http://magickwand.rubyforge.org/