Save 37% off PRO during our Black Friday Sale! »

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

Abda861707b1e78e0fce47ced55f84ee?s=128

Paul Fioravanti

June 12, 2018
Tweet

Transcript

  1. Exercism: Grade School

  2. None
  3. School Roster !

  4. School Roster

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

    for a grade
  6. School Roster ! Add a student's name to the roster

    for a grade ! Get a list of all students enrolled in a grade
  7. 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
  8. ! Take 1

  9. Create a School Roster class School def initialize @roster =

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

    { grade: 1, students: ["Paul", "RobC"] }, { grade: 2, students: ["Nick"]) }, { grade: 3, students: ["Cath", "RobH"]) } ]
  11. Add Student Name to Roster

  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. Get list of students in a grade

  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. Get sorted list of students in all grades

  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. exercism submit grade_school.rb

  34. Done!

  35. Done?

  36. What gives?

  37. What gives? ! Methods are kind of long...

  38. What gives? ! Methods are kind of long... ! Is

    the naming right...?
  39. What gives? ! Methods are kind of long... ! Is

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

    the naming right...? ! There's code repe99on... ! Something doesn't feel right...
  41. ! Take 2

  42. Create a School Roster class School def initialize @roster =

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

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

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

    = {} end private attr_reader :enrollments end
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. Too much repetition

  53. Create a Hash of Enrollments class School def initialize @enrollments

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

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

    = Hash.new { |hash, key| hash[key] = [] } end private attr_reader :enrollments end
  56. 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
  57. Add Student Name to Enrollments def add(name, grade) enrollments[grade] <<

    name end
  58. 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
  59. Get list of students in a grade def students(grade) enrollments[grade].sort

    end
  60. None
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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"]) } ]
  67. You'll rarely get it right the first time

  68. Submit often, get feedback

  69. Read other people's code

  70. Refactor until you're happy

  71. github.com/paulfioravanti/exercism

  72. Thanks! @paulfioravanti