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

Ruby Book Club LIVE

Ruby Book Club LIVE

Hello and welcome to the Ruby Book Club! We’re delighted that you could join us.

Today we’re starting with section 4.9 in Avdi Grimm’s ‘Confident Ruby’: Replace “string typing” with classes.

As always, it doesn’t matter if you haven’t found time to do the reading. At the Ruby Book Club, we like to make sure that everyone is on the same page.

Nadia Odunayo

July 11, 2016
Tweet

More Decks by Nadia Odunayo

Other Decks in Technology

Transcript

  1. Ruby Book Club

    LIVE
    Nadia Odunayo
    @nodunayo
    @rubybookclub

    View Slide

  2. 1 hour of a
    Ruby book
    each week

    View Slide

  3. @saronyitbarek
    codenewbie.org

    View Slide

  4. @nodunayo
    ignition.works

    View Slide

  5. View Slide

  6. 4.9
    Replace “string typing”
    with classes

    View Slide

  7. Interactive!

    View Slide

  8. What did

    you make of the
    reading?

    View Slide

  9. Who’s done the
    reading?

    View Slide

  10. View Slide

  11. View Slide

  12. Let’s get started!

    View Slide

  13. “The string is a stark data structure…a
    perfect vehicle for hiding information.”
    — Alan Perlis
    p.84

    View Slide

  14. Alan J. Perlis
    (April 1, 1922 – February 7, 1990)

    View Slide

  15. String Typing

    View Slide

  16. Replace strings that have special
    meanings with user-defined types.”
    p.84

    View Slide

  17. Input data is represented as a
    specially-formatted String.
    p.84

    View Slide

  18. Input data is represented as a
    specially-formatted String.
    There are numerous case statements
    switching on the content of the
    String.
    p.84

    View Slide

  19. Polymorphism

    View Slide

  20. p.85

    View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. Can you spot any
    problems with this
    code?

    View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. p.87

    View Slide

  33. light = TrafficLight.new
    p.87

    View Slide

  34. light = TrafficLight.new
    light.change_to(“PROCEED”)
    p.87

    View Slide

  35. light = TrafficLight.new
    light.change_to(“PROCEED”)
    puts “Next state: #{light.next_state.inspect}”
    p.87

    View Slide

  36. Next state: nil

    View Slide

  37. View Slide

  38. Next state: nil

    View Slide

  39. None of the case
    statements end with
    else clauses.
    p.87

    View Slide

  40. View Slide

  41. View Slide

  42. Something doesn’t feel
    right…

    View Slide

  43. …so what can we do
    about it?

    View Slide

  44. What if we
    represented traffic
    light states as
    objects?

    View Slide

  45. View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. So, let’s get rid of that
    other case statement…

    View Slide

  50. p.92

    View Slide

  51. Subclasses

    View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. Conclusion

    View Slide

  56. There were repetitive
    case statements all
    switching on the same
    variable.
    p.96

    View Slide

  57. It was too easy to
    introduce an invalid
    value for the @state
    variable.
    p.96

    View Slide

  58. “traffic light state”
    p.96

    View Slide

  59. “The key to working productively in an
    OO language is to make the type
    system and polymorphic method
    dispatch do the work for you.”

    — Avdi Grimm
    p.96

    View Slide

  60. Wrap up

    View Slide

  61. See you next
    week?

    View Slide

  62. Do I need to
    convince you to
    come back next
    week?

    View Slide

  63. Here’s the thing…

    View Slide

  64. How do we

    level up

    as developers?

    View Slide

  65. Read books

    View Slide

  66. How many technical
    books have you

    finished?

    View Slide

  67. Being in a book
    club is the answer.

    View Slide

  68. Focus

    View Slide

  69. Review

    View Slide

  70. Fun

    View Slide

  71. Being in a book
    club is the answer.

    View Slide

  72. Being in the Ruby
    Book Club is the
    answer.

    View Slide

  73. View Slide

  74. CRBOOKCLUB
    20% off
    confidentruby.com

    View Slide

  75. Subscribe

    View Slide

  76. rubybookclub.com

    View Slide

  77. @rubybookclub

    View Slide

  78. See you next week.

    View Slide

  79. See you next week.
    Nadia Odunayo
    @nodunayo
    @rubybookclub
    Thank you!
    rubybookclub.com

    View Slide