Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Last active April 23, 2020 22:43
Show Gist options
  • Save jackcallister/c7623084b5d907f7d155f4caf19bcf08 to your computer and use it in GitHub Desktop.
Save jackcallister/c7623084b5d907f7d155f4caf19bcf08 to your computer and use it in GitHub Desktop.
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