$30 off During Our Annual Pro Sale. View Details »

Stop ignoring pattern matching!

Stop ignoring pattern matching!

Pattern matching was introduced as a major language feature in Ruby 2.7 and has been improved in subsequent releases, but not many people are using it yet. It’s really helpful and can make your programs clearer, simpler, safer, or all three. So why aren’t you using it? You should totally use it! I’ll show you how.

Given at Brighton Ruby 2022. A video is available at https://tomstu.art/stop-ignoring-pattern-matching.

Tom Stuart

June 30, 2022
Tweet

More Decks by Tom Stuart

Other Decks in Technology

Transcript

  1. Tom Stuart, Brighton Ruby 2022
    Stop ignoring pattern matching!
    It’s really good!

    View Slide

  2. ! Hi, I’m Tom, I work at Shopify

    View Slide

  3. “get the most out of Ruby”

    View Slide

  4. Pattern matching

    View Slide

  5. It was added to Ruby 2.7 in 2019

    View Slide

  6. View Slide

  7. It’s really helpful and it can make
    your code clearer, simpler & safer
    It’s really helpful and it can make
    your code clearer, simpler & safer

    View Slide

  8. Here is what I am about to say
    Not including this slide, that would take forever
    • Briefly: why bother?

    • OK OK, what’s pattern matching?

    • The basic bits

    • The intermediate bits

    • The advanced bits

    • Gotchas

    • The end

    View Slide

  9. Briefly: why bother?

    View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. View Slide

  20. View Slide

  21. So why aren’t you using it?

    View Slide

  22. OK OK, what’s pattern matching?

    View Slide

  23. https://docs.ruby-lang.org/en/3.1/syntax/pattern_matching_rdoc.html
    “Pattern matching is a feature allowing
    deep matching of structured values:
    checking the structure and binding the
    matched parts to local variables.”

    View Slide

  24. Recognising values &
    extracting their pieces
    Recognising values &
    extracting their pieces

    View Slide

  25. expression in pattern # returns true or false
    expression => pattern # succeeds or raises
    as of Ruby 3.1.2

    View Slide

  26. case expression
    in pattern
    # …
    in pattern
    # …
    in pattern
    # …
    end # succeeds or raises
    as of Ruby 3.1.2

    View Slide

  27. A completely separate syntax
    A completely separate syntax

    View Slide

  28. Patterns: the basic bits

    View Slide

  29. Value pattern

    View Slide

  30. View Slide

  31. View Slide

  32. Array pattern

    View Slide

  33. View Slide

  34. Hash pattern

    View Slide

  35. View Slide

  36. Alternative pattern

    View Slide

  37. View Slide

  38. Smash them all together!

    View Slide

  39. View Slide

  40. Idea: “case when” → “case in”

    View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. Patterns: the intermediate bits

    View Slide

  46. Rest pattern

    View Slide

  47. View Slide

  48. Variable pattern

    View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. Smash them all together!

    View Slide

  53. View Slide

  54. Real-world examples

    View Slide

  55. View Slide

  56. View Slide

  57. View Slide

  58. Patterns: the advanced bits
    Please read the documentation, it’s good actually
    • Guard clauses
    • Variable and expression pinning
    • Array pattern coercion (#deconstruct)
    • Hash pattern coercion (#deconstruct_keys)
    https://docs.ruby-lang.org/en/3.1/syntax/pattern_matching_rdoc.html

    View Slide

  59. Okay, that was a lot
    about pattern matching

    View Slide

  60. (Maybe this is why
    you’re not using it?)

    View Slide

  61. Gotchas

    View Slide

  62. Rails doesn’t embrace
    pattern matching yet

    View Slide

  63. (Maybe this is why
    you’re not using it?)

    View Slide

  64. View Slide

  65. View Slide

  66. View Slide

  67. View Slide

  68. View Slide

  69. View Slide

  70. Undefined behaviour

    View Slide

  71. e.g. variables in
    unmatched patterns

    View Slide

  72. View Slide

  73. View Slide

  74. “Experimental”…

    View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. “Find patterns” are
    still experimental in Ruby 3.1

    View Slide

  79. View Slide

  80. View Slide

  81. …but Ruby moves on

    View Slide

  82. In summary then

    View Slide

  83. In summary then
    Big picture ideas you can remember
    • Ruby has pattern matching now
    • …and has done for years
    • it can make your code clearer, simpler and safer
    • …so it’s a huge shame to ignore it!
    • don’t be afraid of new Ruby features, and don’t shy away from trying stuff
    even if you don’t see other people doing it — someone has to go first

    View Slide

  84. In summary then
    Concrete things you can actually do
    • try converting “case when” to “case in” to get some easy benefits
    • look for opportunities to match the entire shapes of arrays and hashes instead
    of checking their elements individually
    • once you’re comfortable, dip a toe into more advanced features (guards,
    variable pinning, #deconstruct & #deconstruct_keys, find patterns) to
    get more power out of pattern matching
    • write a conference talk or a blog post and spread the word

    View Slide

  85. https://tomstu.art/stop-ignoring-pattern-matching
    Thanks!

    View Slide