Created
October 9, 2013 12:49
-
-
Save davidbella/6900741 to your computer and use it in GitHub Desktop.
Ruby: School spec passing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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