I’m a new guy with a question. Some background, started learning ruby 4
days ago, and I know nothing other that what I’ve picked up out of a
book I have. I found an exercise that I figured I’d try at the bottom of
this page
http://www.evc-cit.info/cit020/beginning-programming/chp_04/file_printf.html
it looked fun so I tried it. I’d like some feedback from more
experienced guys. What would you have done different?
my code:
#iterate text into variable
text = ‘’
File.open(“scores.txt”).each do |x|
text << x
end
printf(“Name Average Score\n”)
#do math
def do_math(b)
sum = 0
x = b.gsub(“,”, " ")
nums = x.split(/\W/)
#puts nums[0,4]
loopnum = nums.size
arr_size = nums.size.to_i - 1
loopnum.times do |i|
sum = sum + nums[i].to_i
end
#printf(“equals – %i\n”, sum)
printf(“\t\t%3.2f\n”, sum/arr_size.to_f)
end
#handles individual lines
def compare(a)
x = a.size
name = a.gsub(/[0123456789,]/, “”)
printf("\n%s ", name)
garbage = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-”
nums = a.delete(garbage)
do_math(nums) #passes nums to do_math()
end
#split text to array
table = text.split(/\n/)
table_size = table.size
table_size.times do |i| #5 times
compare(table[i]) #pass a line to compare()
end