Skip to content

Instantly share code, notes, and snippets.

@hannesfostie
Created January 31, 2014 14:25
Show Gist options
  • Save hannesfostie/747a3b67f9803bd6a346 to your computer and use it in GitHub Desktop.
Save hannesfostie/747a3b67f9803bd6a346 to your computer and use it in GitHub Desktop.
class School
attr_accessor :db
def initialize
self.db = Hash.new([])
end
def add student, grade
db[grade] << student
end
def grade grade
db[grade]
end
def sort
db.keys.sort.reduce({}) do |new_db, key|
new_db[key] = db[key].sort
new_db
end
end
end
[3] pry(#<SchoolTest>)> school.add 'Aimee', 2
=> ["Aimee"]
[4] pry(#<SchoolTest>)> school
=> #<School:0x007fd34206bdb0 @db={}>
[5] pry(#<SchoolTest>)> school.db
=> {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment