Skip to content

Instantly share code, notes, and snippets.

@vanmichael
Last active December 28, 2015 13:49
Show Gist options
  • Save vanmichael/7510592 to your computer and use it in GitHub Desktop.
Save vanmichael/7510592 to your computer and use it in GitHub Desktop.
#Reads a .txt file and puts each line in array in order to report the average, lowest and highest score.
#List Statistics by Van
require './statistics.rb'
mathmatician = Statistics.new
mathmatician.read_file
puts ""
mathmatician.report_average_score
mathmatician.report_highest_score
mathmatician.report_lowest_score
#Statistics by Van
class Statistics
attr_accessor :scores, :highest_score
def initialize
@scores = []
@total_score = 0
end
def read_file
File.open('scores.txt', 'r').each_line do |r|
puts r
@scores << r
end
end
def report_average_score
@scores.each do |score|
@total_score += score.to_i
end
@average_score = @total_score/(@scores.length + 1)
puts "Average Score: " + @average_score.to_s
end
def report_highest_score
@highest_score = @scores.first.to_i
@scores.each do |score|
@highest_score = score.to_i if score.to_i >= @highest_score
end
puts "Highest Score: " + @highest_score.to_s
end
def report_lowest_score
@lowest_score = @scores.first.to_i
@scores.each do |score|
@lowest_score = score.to_i if score.to_i <= @lowest_score
end
puts "Lowest Score: " + @lowest_score.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment