Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

“get the most out of Ruby”

Slide 4

Slide 4 text

Pattern matching

Slide 5

Slide 5 text

It was added to Ruby 2.7 in 2019

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Briefly: why bother?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

So why aren’t you using it?

Slide 22

Slide 22 text

OK OK, what’s pattern matching?

Slide 23

Slide 23 text

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.”

Slide 24

Slide 24 text

Recognising values & extracting their pieces Recognising values & extracting their pieces

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

A completely separate syntax A completely separate syntax

Slide 28

Slide 28 text

Patterns: the basic bits

Slide 29

Slide 29 text

Value pattern

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Array pattern

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Hash pattern

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Alternative pattern

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Smash them all together!

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Idea: “case when” → “case in”

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Patterns: the intermediate bits

Slide 46

Slide 46 text

Rest pattern

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Variable pattern

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Smash them all together!

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Real-world examples

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

Okay, that was a lot about pattern matching

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Gotchas

Slide 62

Slide 62 text

Rails doesn’t embrace pattern matching yet

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Undefined behaviour

Slide 71

Slide 71 text

e.g. variables in unmatched patterns

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

“Experimental”…

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

“Find patterns” are still experimental in Ruby 3.1

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

…but Ruby moves on

Slide 82

Slide 82 text

In summary then

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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