Skip to content

Instantly share code, notes, and snippets.

@gavinmcgimpsey
Last active January 29, 2016 19:59
Show Gist options
  • Save gavinmcgimpsey/acb51e9c32239377dc36 to your computer and use it in GitHub Desktop.
Save gavinmcgimpsey/acb51e9c32239377dc36 to your computer and use it in GitHub Desktop.
class School
def initialize
@school = {}
end
def add(name, grade)
( @school[grade] &. << name ) || @school[grade] = [name] # Yay 2.3.0!
end
def grade(grade)
@school[grade] || []
end
def to_h
sorted_names = @school.each_with_object( {} ) do |(grade, students), sorted|
sorted[grade] = students.sort
end
Hash[sorted_names.sort]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment