Last active
April 23, 2020 22:43
-
-
Save jackcallister/c7623084b5d907f7d155f4caf19bcf08 to your computer and use it in GitHub Desktop.
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 :classes | |
def initialize | |
@classes = 7.times.map do |i| | |
{ | |
grade: i + 1, | |
students: [] | |
} | |
end | |
end | |
def students(grade) | |
@classes.find { |c| c[:grade] == grade }[:students].sort | |
end | |
def add(name, grade) | |
klass = @classes.find { |c| c[:grade] == grade } | |
klass[:students].push(name) | |
klass[:students] = klass[:students].sort | |
end | |
def students_by_grade | |
@classes.select { |c| c[:students].length > 0 } | |
end | |
end | |
school = School.new | |
%w(Jack John).each { |student| school.add(student, 6) } | |
school.add("Dillon", 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment