From: Josh Cheek Date: 2009-08-29T06:34:43+09:00 Subject: Re: Error with an array of strings --000e0cd356c8e8631e04723a5bc6 Content-Type: text/plain; charset=ISO-8859-1 #Get numbers total = [ ] until $_ == "n" puts "Enter Number:\n" number = STDIN.gets number.chop! number.to_i #this is returning a new int, not converting number to int #you would need to do number = number.to_i #of course, you can do to_f to get a Float total.push(number) #puts all the numbers into the array print "add more numbers to average? y or n\n" continue = STDIN.gets continue.chop! end #Compute Average #multiply by 1.0 to make it a Float (integer division will truncate remainders) #inject just passes some value through each of the indexes #in this case we are passing the sum, it's initial value is zero #so we pass zero as the argument, then in the block, we take #the sum and the number. whatever the block returns will be #plugged in as the sum in the next iteration, and whatever the last #iteration returns will be what the method returns. This is why we do #sum+num.to_f instead of sum += num.to_f averageall = 1.0 * total.inject(0){|sum,num| sum + num.to_f } / total.length print "Average is: ", averageall,"\n" #you're getting puts and print confused __END__ #The below code might be a suitable replacement for your input #though 7stud had a good point, calling that variable "total" is not wise. #You could actually add the value to total, and keep another variable #to track how many inputs there were. That would actually be more efficient #and remove the need for the inject method. total = [] loop do print "Enter Number: " total << gets.to_f #get the number as a float, push it onto total print "Do you have more numbers to average? " break if gets =~ /^n/i #get the input, continue if the first letter isn't n end p total #inspect the input --000e0cd356c8e8631e04723a5bc6--