Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Exercism: Grade School (Ruby)

Exercism: Grade School (Ruby)

A presentation outlining my solution to the Exercism Grade School problem in Ruby (http://exercism.io/exercises/ruby/grade-school/readme).

Presented at the Ruby on Rails Oceania meetup on 12 June 2018.

Presentation slide deck markdown and speaker notes (useable in Deckset 2):
https://github.com/paulfioravanti/presentations/tree/master/exercism_ruby_grade_school

Paul Fioravanti

June 12, 2018
Tweet

More Decks by Paul Fioravanti

Other Decks in Programming

Transcript

  1. School Roster ! Add a student's name to the roster

    for a grade ! Get a list of all students enrolled in a grade
  2. School Roster ! Add a student's name to the roster

    for a grade ! Get a list of all students enrolled in a grade ! Get a sorted list of all students in all grades
  3. Create a School Roster class School def initialize @roster =

    [] end private attr_reader :roster end
  4. A Roster is filled with Enrollments > @roster => [

    { grade: 1, students: ["Paul", "RobC"] }, { grade: 2, students: ["Nick"]) }, { grade: 3, students: ["Cath", "RobH"]) } ]
  5. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  6. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  7. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  8. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  9. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  10. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  11. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  12. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  13. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  14. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  15. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  16. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  17. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  18. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  19. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  20. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  21. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  22. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  23. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  24. What gives? ! Methods are kind of long... ! Is

    the naming right...? ! There's code repe99on...
  25. What gives? ! Methods are kind of long... ! Is

    the naming right...? ! There's code repe99on... ! Something doesn't feel right...
  26. Create a School Roster class School def initialize @roster =

    [] end private attr_reader :roster end
  27. A Roster is filled with Enrollments > @roster => [

    { grade: 1, students: ["Paul", "RobC"] }, { grade: 2, students: ["Nick"]) }, { grade: 3, students: ["Cath", "RobH"]) } ]
  28. Simplify to a Hash of Enrollments > @enrollments => {

    1 => ["Paul", "RobC"], 2 => ["Nick"], 3 => ["Cath", "RobH"] }
  29. Create a Hash of Enrollments class School def initialize @enrollments

    = {} end private attr_reader :enrollments end
  30. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  31. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  32. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  33. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  34. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  35. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  36. Create a Hash of Enrollments class School def initialize @enrollments

    = {} end private attr_reader :enrollments end
  37. Create a Hash of Enrollments class School def initialize @enrollments

    = Hash.new { |hash, key| hash[key] = [] } end private attr_reader :enrollments end
  38. Create a Hash of Enrollments class School def initialize @enrollments

    = Hash.new { |hash, key| hash[key] = [] } end private attr_reader :enrollments end
  39. Add Student Name to Roster def add(name, grade) grade_enrollment =

    roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students] << name else roster << { grade: grade, students: [name] } end end
  40. Get list of students in a grade def students(grade) grade_enrollment

    = roster.find { |enrollment| enrollment[:grade] == grade } if grade_enrollment grade_enrollment[:students].sort else [] end end
  41. Get sorted list of students in all grades def students_by_grade

    roster .sort_by { |enrollment| enrollment[:grade] } .map do |enrollment| enrollment[:students] = enrollment[:students].sort enrollment end end
  42. Get sorted list of students in all grades def students_by_grade

    enrollments.sort.map do |grade, students| { grade: grade, students: students.sort } end end
  43. Get sorted list of students in all grades def students_by_grade

    enrollments.sort.map do |grade, students| { grade: grade, students: students.sort } end end
  44. Get sorted list of students in all grades def students_by_grade

    enrollments.sort.map do |grade, students| { grade: grade, students: students.sort } end end
  45. Get sorted list of students in all grades def students_by_grade

    enrollments.sort.map do |grade, students| { grade: grade, students: students.sort } end end
  46. Get sorted list of students in all grades > school.students_by_grade

    => [ { grade: 1, students: ["Paul", "RobC"] }, { grade: 2, students: ["Nick"]) }, { grade: 3, students: ["Cath", "RobH"]) } ]