Skip to content

Instantly share code, notes, and snippets.

@davidbella
Created October 9, 2013 12:49
Show Gist options
  • Save davidbella/6900741 to your computer and use it in GitHub Desktop.
Save davidbella/6900741 to your computer and use it in GitHub Desktop.
Ruby: School spec passing
class School
attr_accessor :roster
def initialize(name)
@roster = {}
end
def add_student(name, grade)
@roster[grade] ||= []
@roster[grade] << name
end
def grade(grade)
@roster[grade]
end
def sort
# Great explanation of how this works at
# http://blog.jayfields.com/2008/03/ruby-inject.html
# Thanks @ Chris Lee for posting this to Piazza
@roster.sort.inject({}) do |hash, value_pair|
hash[value_pair[0]] = value_pair[1].sort
hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment