Skip to content

Instantly share code, notes, and snippets.

@justuseapen
Created December 2, 2013 19:51
Show Gist options
  • Select an option

  • Save justuseapen/7757013 to your computer and use it in GitHub Desktop.

Select an option

Save justuseapen/7757013 to your computer and use it in GitHub Desktop.
name grade
John Smith 75
Sally Field 85
Jane Doe 93
Gus Grumpy 98
Mark Marcus 68
Vic Victor 83
Frank Furter 89
John Bello 78
Liz Branch 99
require_relative 'student.rb'
require 'csv'
require 'pry'
puts "What is the name of the file which holds the data?"
file_name = gets.chomp
students = []
CSV.foreach(file_name, headers: true) do |row|
students << Student.new(row["name"],row["grade"])
end
#making hash for each group
nineties = []
eighties = []
seventies = []
less_than_70 = []
students.each do |student|
name = student.name
score = student.score.to_i
if score > 90
nineties << name
elsif score > 80
eighties << name
elsif score > 70
seventies << name
else
less_than_70 << name
end
end
puts "90: #{nineties.length}"
puts "80: #{eighties.length}"
puts "70: #{seventies.length}"
puts "<70: #{less_than_70.length}"
puts ""
puts "==== 90+ ===="
puts nineties
puts "==== 80-89 ===="
puts eighties
puts "==== 70-79 ===="
puts seventies
puts "==== <70 ===="
puts less_than_70
class Student
def initialize(name,score)
@name = name
@score = score
end
def name
@name
end
def score
@score
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment