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

Ruby Pattern Matching

Ruby Pattern Matching

Pattern matching is a great way to build Command Line Interfaces (CLIs), deal with third-party Application Programming Interfaces (APIs), and much more. In this presentation you'll learn what pattern matching is and how to leverage the power of pattern matching within your own Ruby applications!

Brooke Kuhlmann

April 18, 2024
Tweet

More Decks by Brooke Kuhlmann

Other Decks in Programming

Transcript

  1. en.wikipedia.org/wiki/Pattern_matching What Pattern matching is the act of checking a

    given sequence of tokens for the presence of the constituents of some pattern. alchemists.io
  2. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] } alchemists.io
  3. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] } alchemists.io
  4. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] } alchemists.io
  5. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] } alchemists.io
  6. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] } alchemists.io
  7. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes The Joy of Pattern Matching! alchemists.io
  8. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => The Joy of Pattern Matching! alchemists.io
  9. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] The Joy of Pattern Matching! alchemists.io
  10. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} The Joy of Pattern Matching! alchemists.io
  11. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder The Joy of Pattern Matching! alchemists.io
  12. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] The Joy of Pattern Matching! alchemists.io
  13. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] The Joy of Pattern Matching! _ alchemists.io
  14. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] **nil The Joy of Pattern Matching! _ alchemists.io
  15. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] **nil ^variable The Joy of Pattern Matching! _ alchemists.io
  16. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] **nil ^variable | The Joy of Pattern Matching! _ alchemists.io
  17. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] **nil ^variable | if, unless The Joy of Pattern Matching! _ alchemists.io
  18. alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes in, => [<pattern>, <pattern>] {key: <pattern>, key: <pattern>} *, **, *remainder, **remainder [*, <pattern>, *] **nil ^variable | if, unless desconstruct, desconstruct_keys The Joy of Pattern Matching! _ alchemists.io
  19. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: alchemists.io alchemists.io/articles/ruby_pattern_matching
  20. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). alchemists.io alchemists.io/articles/ruby_pattern_matching
  21. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). • Standalone boolean checks (i.e. in). alchemists.io alchemists.io/articles/ruby_pattern_matching
  22. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). • Standalone boolean checks (i.e. in). • Find patterns (i.e. <object> in [*, query, \*]). alchemists.io alchemists.io/articles/ruby_pattern_matching
  23. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). • Standalone boolean checks (i.e. in). • Find patterns (i.e. <object> in [*, query, \*]). • Ruby 3.1.0: Rightward assignment and boolean checks became of fi cially supported. alchemists.io alchemists.io/articles/ruby_pattern_matching
  24. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). • Standalone boolean checks (i.e. in). • Find patterns (i.e. <object> in [*, query, \*]). • Ruby 3.1.0: Rightward assignment and boolean checks became of fi cially supported. • Pin operator (i.e. ^) gained the ability to use statements and/or more complex expressions instead of only constants, literals, and local variables. alchemists.io alchemists.io/articles/ruby_pattern_matching
  25. Syntax: Standalone <expression> in <pattern> <expression> => <pattern> 💡 True

    || False alchemists.io alchemists.io/articles/ruby_pattern_matching
  26. Syntax: Standalone <expression> in <pattern> <expression> => <pattern> 💡 True

    || False 💡 Assignment alchemists.io alchemists.io/articles/ruby_pattern_matching
  27. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  28. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  29. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  30. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  31. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  32. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  33. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } true Syntax: Standalone (in) alchemists.io alchemists.io/articles/ruby_pattern_matching
  34. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

    basket.any? { |fruit| fruit in {kind: /app/} } true Syntax: Standalone (in) 🚫 ✅ alchemists.io alchemists.io/articles/ruby_pattern_matching
  35. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) alchemists.io alchemists.io/articles/ruby_pattern_matching
  36. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  37. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) Pattern alchemists.io alchemists.io/articles/ruby_pattern_matching
  38. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) alchemists.io alchemists.io/articles/ruby_pattern_matching
  39. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) alchemists.io alchemists.io/articles/ruby_pattern_matching
  40. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Syntax: Standalone (=>) alchemists.io alchemists.io/articles/ruby_pattern_matching
  41. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

    Name: #{last_name}" Last Name: Reynolds Syntax: Standalone (=>) alchemists.io alchemists.io/articles/ruby_pattern_matching
  42. Syntax: Case case <expression> in <pattern> then ... in <pattern>

    then ... in <pattern> then ... else ... end alchemists.io alchemists.io/articles/ruby_pattern_matching
  43. Syntax: Case case <expression> in <pattern> then ... in <pattern>

    then ... in <pattern> then ... else ... end alchemists.io alchemists.io/articles/ruby_pattern_matching
  44. case <expression> in <pattern> then ... when <pattern> then ...

    else ... end Syntax: Case alchemists.io alchemists.io/articles/ruby_pattern_matching
  45. case <expression> in <pattern> then ... when <pattern> then ...

    else ... end Syntax: Case alchemists.io alchemists.io/articles/ruby_pattern_matching
  46. c ccc c cc c c c c ccccc e

    ee Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  47. case <expression> when <pattern> then ... when <pattern> then ...

    when <pattern> then ... end case <expression> in <pattern> then ... in <pattern> then ... in <pattern> then ... end Control Flow Pattern Matching Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  48. case <expression> when <pattern> then ... when <pattern> then ...

    when <pattern> then ... end case <expression> in <pattern> then ... in <pattern> then ... in <pattern> then ... end Control Flow Pattern Matching Nothing Happens 😢 Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  49. case <expression> when <pattern> then ... when <pattern> then ...

    when <pattern> then ... end case <expression> in <pattern> then ... in <pattern> then ... in <pattern> then ... end Control Flow Pattern Matching Nothing Happens 😢 NoMatchingPatternError 🎉 Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  50. case <expression> when <pattern> then ... when <pattern> then ...

    when <pattern> then ... end case <expression> in <pattern> then ... in <pattern> then ... in <pattern> then ... end Control Flow Pattern Matching Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  51. case <expression> when <pattern> then ... when <pattern> then ...

    when <pattern> then ... end case <expression> in <pattern> then ... in <pattern> then ... in <pattern> then ... end Control Flow Pattern Matching Style/MissingElse: Enabled: true Syntax: Case (Exhaustive Matching) alchemists.io alchemists.io/articles/ruby_pattern_matching
  52. case [1, 2, 3] in [Integer, Integer] then "match" else

    "unmatched" end case {a: 1, b: 2, c: 3} in {a: Integer} then "matched" else "unmatched" end Syntax: Optional alchemists.io alchemists.io/articles/ruby_pattern_matching
  53. case [1, 2, 3] in Integer, Integer then "match" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end Syntax: Optional alchemists.io alchemists.io/articles/ruby_pattern_matching
  54. case [1, 2, 3] in Integer, Integer then "match" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end Syntax: Optional alchemists.io alchemists.io/articles/ruby_pattern_matching
  55. case [1, 2, 3] in Integer, Integer then "match" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end Syntax: Optional ⚠ Required for standalone syntax alchemists.io alchemists.io/articles/ruby_pattern_matching
  56. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end Array Hash Whole Versus Partial alchemists.io alchemists.io/articles/ruby_pattern_matching
  57. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end "unmatched" Array Hash Whole Versus Partial alchemists.io alchemists.io/articles/ruby_pattern_matching
  58. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end "unmatched" Array Hash Whole Versus Partial alchemists.io alchemists.io/articles/ruby_pattern_matching
  59. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end "unmatched" Array Hash Whole Versus Partial 💡 Order Matters alchemists.io alchemists.io/articles/ruby_pattern_matching
  60. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end "unmatched" "matched" Array Hash Whole Versus Partial alchemists.io alchemists.io/articles/ruby_pattern_matching
  61. case [1, 2, 3] in Integer, Integer then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer then "matched" else "unmatched" end "unmatched" "matched" Array Hash Whole Versus Partial alchemists.io alchemists.io/articles/ruby_pattern_matching
  62. case {a: 1, b: 2, c: 3} in {} then

    "matched" else "unmatched" end case {} in {} then "matched" else "unmatched" end With Content Without Content Caveat: Empty Hashes alchemists.io alchemists.io/articles/ruby_pattern_matching
  63. case {a: 1, b: 2, c: 3} in {} then

    "matched" else "unmatched" end case {} in {} then "matched" else "unmatched" end With Content Without Content Caveat: Empty Hashes alchemists.io alchemists.io/articles/ruby_pattern_matching
  64. case {a: 1, b: 2, c: 3} in {} then

    "matched" else "unmatched" end case {} in {} then "matched" else "unmatched" end "unmatched" With Content Without Content Caveat: Empty Hashes alchemists.io alchemists.io/articles/ruby_pattern_matching
  65. case {a: 1, b: 2, c: 3} in {} then

    "matched" else "unmatched" end case {} in {} then "matched" else "unmatched" end "unmatched" With Content Without Content Caveat: Empty Hashes alchemists.io alchemists.io/articles/ruby_pattern_matching
  66. case {a: 1, b: 2, c: 3} in {} then

    "matched" else "unmatched" end case {} in {} then "matched" else "unmatched" end "unmatched" "matched" With Content Without Content Caveat: Empty Hashes alchemists.io alchemists.io/articles/ruby_pattern_matching
  67. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

    * then "matched" else "unmatched" end Arrays Only Finds alchemists.io alchemists.io/articles/ruby_pattern_matching
  68. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

    * then "matched" else "unmatched" end Arrays Only Finds alchemists.io alchemists.io/articles/ruby_pattern_matching
  69. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

    * then "matched" else "unmatched" end Arrays Only Finds alchemists.io alchemists.io/articles/ruby_pattern_matching
  70. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

    * then "matched" else "unmatched" end "matched" Arrays Only Finds alchemists.io alchemists.io/articles/ruby_pattern_matching
  71. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

    * then "matched" else "unmatched" end "matched" Arrays Only Finds ⚠ Expensive alchemists.io alchemists.io/articles/ruby_pattern_matching
  72. case [:a, 1, :b, :c, 2] in *first, Symbol, Symbol,

    *last then {first: first, last: last} else "unmatched" end Arrays Only Finds ⚠ Expensive alchemists.io alchemists.io/articles/ruby_pattern_matching
  73. case [:a, 1, :b, :c, 2] in *first, Symbol, Symbol,

    *last then {first: first, last: last} else "unmatched" end Arrays Only Finds ⚠ Expensive alchemists.io alchemists.io/articles/ruby_pattern_matching
  74. case [:a, 1, :b, :c, 2] in *first, Symbol, Symbol,

    *last then {first: first, last: last} else "unmatched" end { fi rst: [:a, 1], last: [2]} Arrays Only Finds ⚠ Expensive alchemists.io alchemists.io/articles/ruby_pattern_matching
  75. case [:a, 1, :b, :c, 2] in *first, Symbol, Symbol,

    *last then {first: first, last: last} else "unmatched" end { fi rst: [:a, 1], last: [2]} Arrays Only Finds ⚠ Expensive alchemists.io alchemists.io/articles/ruby_pattern_matching
  76. case {a: 1, b: 2} in {a: Integer, **nil} then

    %(matched "a" part) in {a: Integer, b: Integer, **nil} then "matched whole hash" else "unmatched" end Hashes Only Voids alchemists.io alchemists.io/articles/ruby_pattern_matching
  77. case {a: 1, b: 2} in {a: Integer, **nil} then

    %(matched "a" part) in {a: Integer, b: Integer, **nil} then "matched whole hash" else "unmatched" end "matched whole hash" Hashes Only Voids alchemists.io alchemists.io/articles/ruby_pattern_matching
  78. case {a: 1, b: 2} in {a: Integer, **nil} then

    %(matched "a" part) in {a: Integer, b: Integer, **nil} then "matched whole hash" else "unmatched" end "matched whole hash" Hashes Only Voids alchemists.io alchemists.io/articles/ruby_pattern_matching
  79. case {a: 1, b: 2} in {a: Integer, **nil} then

    %(matched "a" part) in {a: Integer, b: Integer, **nil} then "matched whole hash" else "unmatched" end "matched whole hash" Hashes Only ⚠ Pattern end only Voids alchemists.io alchemists.io/articles/ruby_pattern_matching
  80. case {a: 1, b: 2} in {a: Integer, **nil} then

    %(matched "a" part) in {a: Integer, b: Integer, **nil} then "matched whole hash" else "unmatched" end "matched whole hash" Hashes Only Voids ✅ alchemists.io alchemists.io/articles/ruby_pattern_matching
  81. case [1, 2, 3] in Integer, * then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer, ** then "matched" else "unmatched" end Array Hash Remainders alchemists.io alchemists.io/articles/ruby_pattern_matching
  82. case [1, 2, 3] in Integer, * then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer, ** then "matched" else "unmatched" end Array Hash Remainders alchemists.io alchemists.io/articles/ruby_pattern_matching
  83. case [1, 2, 3] in Integer, * then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer, ** then "matched" else "unmatched" end "matched" Array Hash Remainders alchemists.io alchemists.io/articles/ruby_pattern_matching
  84. case [1, 2, 3] in Integer, * then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer, ** then "matched" else "unmatched" end "matched" Array Hash Remainders alchemists.io alchemists.io/articles/ruby_pattern_matching
  85. case [1, 2, 3] in Integer, * then "matched" else

    "unmatched" end case {a: 1, b: 2, c: 3} in a: Integer, ** then "matched" else "unmatched" end "matched" "matched" Array Hash Remainders alchemists.io alchemists.io/articles/ruby_pattern_matching
  86. case [1, 2] in Integer => first, Integer then "matched:

    #{first}" else "unmatched" end case {a: 1, b: 2} in a: Integer => first then "matched: #{first}" else "unmatched" end Array Variable Binding Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  87. case [1, 2] in Integer => first, Integer then "matched:

    #{first}" else "unmatched" end case {a: 1, b: 2} in a: Integer => first then "matched: #{first}" else "unmatched" end Array Variable Binding Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  88. case [1, 2] in Integer => first, Integer then "matched:

    #{first}" else "unmatched" end case {a: 1, b: 2} in a: Integer => first then "matched: #{first}" else "unmatched" end "matched: 1" Array Variable Binding Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  89. case [1, 2] in Integer => first, Integer then "matched:

    #{first}" else "unmatched" end case {a: 1, b: 2} in a: Integer => first then "matched: #{first}" else "unmatched" end "matched: 1" Array Variable Binding Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  90. case [1, 2] in Integer => first, Integer then "matched:

    #{first}" else "unmatched" end case {a: 1, b: 2} in a: Integer => first then "matched: #{first}" else "unmatched" end "matched: 1" "matched: 1" Array Variable Binding Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  91. case [1, 2] in first, Integer then "matched: #{first}" else

    "unmatched" end case {a: 1, b: 2} in a: first then "matched: #{first}" else "unmatched" end Array Hash Variable Binding: Simpli fi ed alchemists.io alchemists.io/articles/ruby_pattern_matching
  92. case [1, 2] in first, Integer then "matched: #{first}" else

    "unmatched" end case {a: 1, b: 2} in a: first then "matched: #{first}" else "unmatched" end Array Hash Variable Binding: Simpli fi ed alchemists.io alchemists.io/articles/ruby_pattern_matching
  93. case [1, 2] in first, Integer then "matched: #{first}" else

    "unmatched" end case {a: 1, b: 2} in a: first then "matched: #{first}" else "unmatched" end "matched: 1" Array Hash Variable Binding: Simpli fi ed alchemists.io alchemists.io/articles/ruby_pattern_matching
  94. case [1, 2] in first, Integer then "matched: #{first}" else

    "unmatched" end case {a: 1, b: 2} in a: first then "matched: #{first}" else "unmatched" end "matched: 1" Array Hash Variable Binding: Simpli fi ed alchemists.io alchemists.io/articles/ruby_pattern_matching
  95. case [1, 2] in first, Integer then "matched: #{first}" else

    "unmatched" end case {a: 1, b: 2} in a: first then "matched: #{first}" else "unmatched" end "matched: 1" "matched: 1" Array Hash Variable Binding: Simpli fi ed alchemists.io alchemists.io/articles/ruby_pattern_matching
  96. case {a: 1, b: 2} in a: then "matched: #{a}"

    else "unmatched" end Variable Binding: Hash Only alchemists.io alchemists.io/articles/ruby_pattern_matching
  97. case {a: 1, b: 2} in a: then "matched: #{a}"

    else "unmatched" end Variable Binding: Hash Only alchemists.io alchemists.io/articles/ruby_pattern_matching
  98. case {a: 1, b: 2} in a: then "matched: #{a}"

    else "unmatched" end "matched: 1" Variable Binding: Hash Only alchemists.io alchemists.io/articles/ruby_pattern_matching
  99. case [%w[Apple Apricot], %w[Blueberry Blackberry]] in [[first, String], *] then

    "matched: #{first}" else "unmatched" end case {label: "Basket", fruits: [{label: "Apple"}, {label: "Peach"}]} in label:, fruits: [{label: first}, *] then "matched: #{first}" else "unmatched" end Array Hash Explicit Brackets Variable Binding: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  100. case [%w[Apple Apricot], %w[Blueberry Blackberry]] in [[first, String], *] then

    "matched: #{first}" else "unmatched" end case {label: "Basket", fruits: [{label: "Apple"}, {label: "Peach"}]} in label:, fruits: [{label: first}, *] then "matched: #{first}" else "unmatched" end Array Hash Explicit Brackets Variable Binding: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  101. case [%w[Apple Apricot], %w[Blueberry Blackberry]] in [[first, String], *] then

    "matched: #{first}" else "unmatched" end case {label: "Basket", fruits: [{label: "Apple"}, {label: "Peach"}]} in label:, fruits: [{label: first}, *] then "matched: #{first}" else "unmatched" end "matched: Apple" "matched: Apple" Array Hash Explicit Brackets Variable Binding: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  102. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  103. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  104. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  105. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end "matched: 1, [2, 3]" Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  106. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end "matched: 1, [2, 3]" Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  107. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end "matched: 1, [2, 3]" Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  108. case [1, 2, 3] in first, *remainder then "matched: #{first},

    #{remainder}" else "unmatched" end case {a: 1, b: 2, c: 3} in a:, **remainder then "matched: #{a}, #{remainder}" else "unmatched" end "matched: 1, {:b=>2, :c=>3}" "matched: 1, [2, 3]" Array Hash Variable Binding: Remainder alchemists.io alchemists.io/articles/ruby_pattern_matching
  109. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end Variable Pinning alchemists.io alchemists.io/articles/ruby_pattern_matching
  110. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end Variable Pinning alchemists.io alchemists.io/articles/ruby_pattern_matching
  111. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end Variable Pinning alchemists.io alchemists.io/articles/ruby_pattern_matching
  112. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end Variable Pinning alchemists.io alchemists.io/articles/ruby_pattern_matching
  113. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end "matched: 1" Variable Pinning alchemists.io alchemists.io/articles/ruby_pattern_matching
  114. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end "matched: 1" Variable Pinning `expectation` is pinned to `1` alchemists.io alchemists.io/articles/ruby_pattern_matching
  115. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end "matched: 1" "unmatched: 5" Variable Pinning `expectation` is pinned to `1` alchemists.io alchemists.io/articles/ruby_pattern_matching
  116. expectation = 5 case [1, 2] in expectation, * then

    "matched: #{expectation}" else "unmatched: #{expectation}" end expectation = 5 case [1, 2] in ^expectation, * then "matched: #{expectation}" else "unmatched: #{expectation}" end "matched: 1" "unmatched: 5" Variable Pinning `expectation` is pinned to `1` `expectation` is pinned to `5` alchemists.io alchemists.io/articles/ruby_pattern_matching
  117. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end Variable Pinning: Comparison alchemists.io alchemists.io/articles/ruby_pattern_matching
  118. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" Variable Pinning: Comparison alchemists.io alchemists.io/articles/ruby_pattern_matching
  119. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" "values are different" Variable Pinning: Comparison alchemists.io alchemists.io/articles/ruby_pattern_matching
  120. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" "values are different" Variable Pinning: Comparison 1 alchemists.io alchemists.io/articles/ruby_pattern_matching
  121. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" "values are different" Variable Pinning: Comparison 1 1 alchemists.io alchemists.io/articles/ruby_pattern_matching
  122. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" "values are different" Variable Pinning: Comparison 1 1 alchemists.io alchemists.io/articles/ruby_pattern_matching
  123. case [1, 1] in value, ^value then "values are identical"

    else "values are different" end case [1, 2] in value, ^value then "values are identical" else "values are different" end "values are identical" "values are different" Variable Pinning: Comparison 1 1 1 1 alchemists.io alchemists.io/articles/ruby_pattern_matching
  124. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  125. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  126. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" Key Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  127. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" Pin Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  128. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" Value Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  129. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" Value Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  130. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" ✅ Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  131. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" "unmatched" Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  132. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" "unmatched" Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  133. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" "unmatched" Pin ("high") Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  134. case {school: "high", schools: [{id: 1, level: "middle"}, {id: 2,

    level: "high"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end case {school: "high", schools: [{id: 1, level: "middle"}]} in school:, schools: [*, {id:, level: ^school}] then "matched: #{id}" else "unmatched" end "matched: 2" "unmatched" Pin ("high") "middle" != "high" Variable Pinning: Nested alchemists.io alchemists.io/articles/ruby_pattern_matching
  135. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end Variable Pinning: Expression alchemists.io alchemists.io/articles/ruby_pattern_matching
  136. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end Variable Pinning: Expression alchemists.io alchemists.io/articles/ruby_pattern_matching
  137. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end Variable Pinning: Expression alchemists.io alchemists.io/articles/ruby_pattern_matching
  138. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression alchemists.io alchemists.io/articles/ruby_pattern_matching
  139. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression multiplier = 2 case [2, 4] in Integer, ^(2 * multiplier) => expectation then "matched: #{expectation}" else "unmatched: #{expectation}" end alchemists.io alchemists.io/articles/ruby_pattern_matching
  140. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression multiplier = 2 case [2, 4] in Integer, ^(2 * multiplier) => expectation then "matched: #{expectation}" else "unmatched: #{expectation}" end alchemists.io alchemists.io/articles/ruby_pattern_matching
  141. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression multiplier = 2 case [2, 4] in Integer, ^(2 * multiplier) => expectation then "matched: #{expectation}" else "unmatched: #{expectation}" end alchemists.io alchemists.io/articles/ruby_pattern_matching
  142. multiplier = 2 case [2, 4] in Integer, ^(2 *

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression multiplier = 2 case [2, 4] in Integer, ^(2 * multiplier) => expectation then "matched: #{expectation}" else "unmatched: #{expectation}" end "matched: 4" alchemists.io alchemists.io/articles/ruby_pattern_matching
  143. Combinations case [:a, 1, 2.0] in String | Symbol, Integer,

    Float then "matched" else "unmatched" end case {a: 1, b: 2, c: 3} in Array | {a: Integer} then "matched" else "unmatched" end Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  144. Combinations case [:a, 1, 2.0] in String | Symbol, Integer,

    Float then "matched" else "unmatched" end case {a: 1, b: 2, c: 3} in Array | {a: Integer} then "matched" else "unmatched" end Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  145. Combinations case [:a, 1, 2.0] in String | Symbol, Integer,

    Float then "matched" else "unmatched" end case {a: 1, b: 2, c: 3} in Array | {a: Integer} then "matched" else "unmatched" end "matched" Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  146. Combinations case [:a, 1, 2.0] in String | Symbol, Integer,

    Float then "matched" else "unmatched" end case {a: 1, b: 2, c: 3} in Array | {a: Integer} then "matched" else "unmatched" end "matched" "matched" Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  147. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end alchemists.io alchemists.io/articles/ruby_pattern_matching
  148. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end illegal variable in alternative pattern (a) (SyntaxError) Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end 🚫 alchemists.io alchemists.io/articles/ruby_pattern_matching
  149. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end illegal variable in alternative pattern (a) (SyntaxError) Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end 🚫 alchemists.io alchemists.io/articles/ruby_pattern_matching
  150. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end illegal variable in alternative pattern (a) (SyntaxError) Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end matched 🚫 🚫 alchemists.io alchemists.io/articles/ruby_pattern_matching
  151. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end illegal variable in alternative pattern (a) (SyntaxError) Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end matched 🚫 🚫 alchemists.io alchemists.io/articles/ruby_pattern_matching
  152. Combinations case {a: 1, b: 2, c: 3} in {a:}

    | Array then "matched" else "unmatched" end illegal variable in alternative pattern (a) (SyntaxError) Hash Only case {a: 1, b: 2, c: 3} in {a: _} | Array then "matched" else "unmatched" end matched 🚫 🚫 Meant to signify discarded values only! alchemists.io alchemists.io/articles/ruby_pattern_matching
  153. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  154. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end "matched" "matched" Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  155. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end "matched" "matched" Array Hash alchemists.io alchemists.io/articles/ruby_pattern_matching
  156. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end "matched" "matched" Array Hash 💡`unless` works too! alchemists.io alchemists.io/articles/ruby_pattern_matching
  157. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end "matched" "matched" Array Hash 💡`unless` works too! alchemists.io alchemists.io/articles/ruby_pattern_matching
  158. Guards case %w[apple peach blueberry] in String, middle, String if

    middle == "peach" then "matched" else "unmatched" end case {animal: "bat", vehicle: "tumbler", color: "black"} in {color: String} if color == "black" then "matched" else "unmatched" end "matched" "matched" Array Hash 💡`unless` works too! alchemists.io alchemists.io/articles/ruby_pattern_matching
  159. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  160. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end Array Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  161. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end Array Hash Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  162. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end Array Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  163. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end # => "matched: 1" Array Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  164. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end # => "matched: 1" Array Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  165. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end # => "matched: 1" case Point.new 1, -2 in x: 0.. then "matched: #{x}" else "unmatched" end Array Pattern Hash Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  166. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end # => "matched: 1" case Point.new 1, -2 in x: 0.. then "matched: #{x}" else "unmatched" end # => "matched: 1" Array Pattern Hash Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  167. class Point attr_reader :x, :y def initialize x, y @x

    = x @y = y end def deconstruct = [x, y] def deconstruct_keys(keys) = {x: x, y: y} end case Point.new 1, -2 in x, Integer then "matched: #{x}" else "unmatched" end # => "matched: 1" case Point.new 1, -2 in x: 0.. then "matched: #{x}" else "unmatched" end # => "matched: 1" Array Pattern Hash Pattern Classes alchemists.io alchemists.io/articles/ruby_pattern_matching
  168. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

    end Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  169. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  170. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y Same Type alchemists.io alchemists.io/articles/ruby_pattern_matching
  171. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y Point[..5, ..5] alchemists.io alchemists.io/articles/ruby_pattern_matching
  172. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y Point[..5, ..5] Type[pattern] alchemists.io alchemists.io/articles/ruby_pattern_matching
  173. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  174. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y "matched" alchemists.io alchemists.io/articles/ruby_pattern_matching
  175. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y "matched" Same Type alchemists.io alchemists.io/articles/ruby_pattern_matching
  176. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case [1, 2] in Point[..5, ..5] then "matched" else "unmatched" end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y "matched" alchemists.io alchemists.io/articles/ruby_pattern_matching
  177. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case [1, 2] in Point[..5, ..5] then "matched" else "unmatched" end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y "matched" "unmatched" alchemists.io alchemists.io/articles/ruby_pattern_matching
  178. case Point[1, 2] in ..5, ..5 then "matched" else "unmatched"

    end case [1, 2] in Point[..5, ..5] then "matched" else "unmatched" end case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched" end "matched" Structs Point = Struct.new :x, :y Wrong Type "matched" "unmatched" alchemists.io alchemists.io/articles/ruby_pattern_matching
  179. case Point[1, 2] in Point(x:, y:) then "matched" else "unmatched"

    end Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  180. case Point[1, 2] in Point(x:, y:) then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  181. case Point[1, 2] in Point(x:, y:) then "matched" else "unmatched"

    end "matched" Structs Point = Struct.new :x, :y alchemists.io alchemists.io/articles/ruby_pattern_matching
  182. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  183. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  184. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  185. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  186. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  187. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  188. module Rubysmith module CLI # The main Command Line Interface

    (CLI) object. class Shell # Truncated. def call arguments = [] case parse arguments in action_config: Symbol => action then config.call action in action_build: true then build.call in action_version: true then logger.info { configuration.version } else logger.any { parser.to_s } end end end end end Rubysmith alchemists.io alchemists.io/projects/rubysmith
  189. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  190. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  191. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  192. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  193. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  194. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  195. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  196. module Janus module Actions class Processor # Trunctated. def call

    request # Trunctated. text = request.text case text.split in ["help"] then help in "status" => status, String => name then status.call name in "version", String => name, String => version then version.call name, version in "deploy", String => name, String => version then deploy.call name, version else unknown.call text end end end end end Janus alchemists.io alchemists.io/talks/janus
  197. Guidelines • Use sorted arrays. • Use limited fi nds:

    [*, <pattern>, *]. alchemists.io/articles/ruby_pattern_matching alchemists.io
  198. Guidelines • Use sorted arrays. • Use limited fi nds:

    [*, <pattern>, *]. • Avoid unnecessary deconstruction. alchemists.io/articles/ruby_pattern_matching alchemists.io
  199. Guidelines • Use sorted arrays. • Use limited fi nds:

    [*, <pattern>, *]. • Avoid unnecessary deconstruction. • Avoid shadow variables. alchemists.io/articles/ruby_pattern_matching alchemists.io
  200. Guidelines • Use sorted arrays. • Use limited fi nds:

    [*, <pattern>, *]. • Avoid unnecessary deconstruction. • Avoid shadow variables. • Avoid complicated nested patterns. alchemists.io/articles/ruby_pattern_matching alchemists.io