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

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

October 01, 2022
Tweet

More Decks by Brooke Kuhlmann

Other Decks in Programming

Transcript

  1. Ruby Pattern Matching Presented by Brooke Kuhlmann

  2. https://en.wikipedia.org/wiki/Pattern_matching What

  3. https://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.
  4. What

  5. What { ship: "Serenity", crew: [ { name: "Malcolm Reynolds",

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

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

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

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

    title: "Captain" }, { name: "Zoe Alleyne Washburne", title: "Fighter" }, { name: "Kaywinnet Lee Frye" title: "Engineer" } ] }
  10. https://www.alchemists.io/articles/ruby_pattern_matching Overview

  11. https://www.alchemists.io/articles/ruby_pattern_matching Overview • Standalone • Arrays • Hashes • Remainders

    • Finds • Excludes • Voids • Variable Binding • Combinations • Guards • Classes The Joy of Pattern Matching!
  12. https://www.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!
  13. https://www.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!
  14. https://www.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!
  15. https://www.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!
  16. https://www.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!
  17. https://www.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! _
  18. https://www.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! _
  19. https://www.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! _
  20. https://www.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! _
  21. https://www.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! _
  22. https://www.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! _
  23. History https://www.alchemists.io/articles/ruby_pattern_matching

  24. History • Ruby 2.7.0: Introduced https://www.alchemists.io/articles/ruby_pattern_matching

  25. History • Ruby 2.7.0: Introduced • Ruby 3.0.0: Of fi

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

    cially supported with new experimental features: • Standalone rightward assignment (i.e. =>). https://www.alchemists.io/articles/ruby_pattern_matching
  27. 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). https://www.alchemists.io/articles/ruby_pattern_matching
  28. 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, \*]). https://www.alchemists.io/articles/ruby_pattern_matching
  29. 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. https://www.alchemists.io/articles/ruby_pattern_matching
  30. 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. https://www.alchemists.io/articles/ruby_pattern_matching
  31. Syntax: Standalone

  32. Syntax: Standalone <expression> in <pattern> <expression> => <pattern> https://www.alchemists.io/articles/ruby_pattern_matching

  33. Syntax: Standalone <expression> in <pattern> <expression> => <pattern> 💡 True

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

    || False 💡 Assignment https://www.alchemists.io/articles/ruby_pattern_matching
  35. Syntax: Standalone (in) https://www.alchemists.io/articles/ruby_pattern_matching

  36. basket = [{kind: "apple", quantity: 1}, {kind: "peach", quantity: 5}]

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

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

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

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

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

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

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

    basket.any? { |fruit| fruit in {kind: /app/} } true Syntax: Standalone (in) 🚫 ✅ https://www.alchemists.io/articles/ruby_pattern_matching
  44. Syntax: Standalone (=>) https://www.alchemists.io/articles/ruby_pattern_matching

  45. {character: {first_name: "Malcolm", last_name: "Reynolds"}} => {character: {last_name:}} puts "Last

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

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

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

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

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

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

    Name: #{last_name}" Last Name: Reynolds Syntax: Standalone (=>) https://www.alchemists.io/articles/ruby_pattern_matching
  52. Syntax: Case

  53. Syntax: Case case <expression> in <pattern> then ... in <pattern>

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

    then ... in <pattern> then ... else ... end https://www.alchemists.io/articles/ruby_pattern_matching
  55. case <expression> in <pattern> then ... when <pattern> then ...

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

    else ... end Syntax: Case https://www.alchemists.io/articles/ruby_pattern_matching
  57. Syntax: Case https://www.alchemists.io/articles/ruby_pattern_matching

  58. c ccc c cc c c c c ccccc e

    ee Syntax: Case (Exhaustive Matching) https://www.alchemists.io/articles/ruby_pattern_matching
  59. NoMatchingPatternError Syntax: Case (Exhaustive Matching) https://www.alchemists.io/articles/ruby_pattern_matching

  60. NoMatchingPatternError ♥ Syntax: Case (Exhaustive Matching) https://www.alchemists.io/articles/ruby_pattern_matching

  61. NoMatchingPatternError ♥ Syntax: Case (Exhaustive Matching) https://www.alchemists.io/articles/ruby_pattern_matching

  62. Control Flow Pattern Matching Syntax: Case (Exhaustive Matching) https://www.alchemists.io/articles/ruby_pattern_matching

  63. 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) https://www.alchemists.io/articles/ruby_pattern_matching
  64. 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) https://www.alchemists.io/articles/ruby_pattern_matching
  65. 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) https://www.alchemists.io/articles/ruby_pattern_matching
  66. 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) https://www.alchemists.io/articles/ruby_pattern_matching
  67. 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) https://www.alchemists.io/articles/ruby_pattern_matching
  68. Syntax: Optional https://www.alchemists.io/articles/ruby_pattern_matching

  69. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  70. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  71. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  72. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  73. Whole Versus Partial https://www.alchemists.io/articles/ruby_pattern_matching

  74. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  75. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  76. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  77. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  78. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  79. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  80. Caveat: Empty Hashes https://www.alchemists.io/articles/ruby_pattern_matching

  81. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  82. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  83. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  84. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  85. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  86. Finds https://www.alchemists.io/articles/ruby_pattern_matching

  87. case [:a, 1, :b, :c, 2] in *, Symbol, Symbol,

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

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

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

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

    * then "matched" else "unmatched" end "matched" Arrays Only ⚠ Experimental Finds ⚠ Expensive https://www.alchemists.io/articles/ruby_pattern_matching
  92. ⚠ Experimental Finds ⚠ Expensive https://www.alchemists.io/articles/ruby_pattern_matching

  93. case [:a, 1, :b, :c, 2] in *first, Symbol, Symbol,

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

    *last then {first: first, last: last} else "unmatched" end Arrays Only ⚠ Experimental Finds ⚠ Expensive https://www.alchemists.io/articles/ruby_pattern_matching
  95. 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 ⚠ Experimental Finds ⚠ Expensive https://www.alchemists.io/articles/ruby_pattern_matching
  96. 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 ⚠ Experimental Finds ⚠ Expensive https://www.alchemists.io/articles/ruby_pattern_matching
  97. Voids https://www.alchemists.io/articles/ruby_pattern_matching

  98. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  99. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  100. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  101. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  102. 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 ✅ https://www.alchemists.io/articles/ruby_pattern_matching
  103. Remainders https://www.alchemists.io/articles/ruby_pattern_matching

  104. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  105. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  106. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  107. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  108. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  109. Variable Binding https://www.alchemists.io/articles/ruby_pattern_matching

  110. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  111. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  112. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  113. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  114. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  115. Variable Binding: Simpli fi ed https://www.alchemists.io/articles/ruby_pattern_matching

  116. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  117. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  118. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  119. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  120. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  121. Variable Binding: Hash Only https://www.alchemists.io/articles/ruby_pattern_matching

  122. case {a: 1, b: 2} in a: then "matched: #{a}"

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

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

    else "unmatched" end "matched: 1" Variable Binding: Hash Only https://www.alchemists.io/articles/ruby_pattern_matching
  125. Explicit Brackets Variable Binding: Nested https://www.alchemists.io/articles/ruby_pattern_matching

  126. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  127. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  128. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  129. Variable Binding: Remainder https://www.alchemists.io/articles/ruby_pattern_matching

  130. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  131. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  132. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  133. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  134. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  135. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  136. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  137. Variable Pinning https://www.alchemists.io/articles/ruby_pattern_matching

  138. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  139. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  140. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  141. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  142. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  143. 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` https://www.alchemists.io/articles/ruby_pattern_matching
  144. 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` https://www.alchemists.io/articles/ruby_pattern_matching
  145. 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` https://www.alchemists.io/articles/ruby_pattern_matching
  146. Variable Pinning: Comparison https://www.alchemists.io/articles/ruby_pattern_matching

  147. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  148. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  149. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  150. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  151. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  152. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  153. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  154. Variable Pinning: Nested https://www.alchemists.io/articles/ruby_pattern_matching

  155. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  156. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  157. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  158. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  159. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  160. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  161. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  162. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  163. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  164. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  165. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  166. Variable Pinning: Expression https://www.alchemists.io/articles/ruby_pattern_matching

  167. multiplier = 2 case [2, 4] in Integer, ^(2 *

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

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

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

    multiplier) then "matched" else "unmatched" end "matched" Variable Pinning: Expression https://www.alchemists.io/articles/ruby_pattern_matching
  171. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  172. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  173. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  174. 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" https://www.alchemists.io/articles/ruby_pattern_matching
  175. Combinations https://www.alchemists.io/articles/ruby_pattern_matching

  176. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  177. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  178. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  179. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  180. Combinations https://www.alchemists.io/articles/ruby_pattern_matching

  181. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  182. 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 🚫 https://www.alchemists.io/articles/ruby_pattern_matching
  183. 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 🚫 https://www.alchemists.io/articles/ruby_pattern_matching
  184. 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 🚫 🚫 https://www.alchemists.io/articles/ruby_pattern_matching
  185. 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 🚫 🚫 https://www.alchemists.io/articles/ruby_pattern_matching
  186. 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! https://www.alchemists.io/articles/ruby_pattern_matching
  187. Guards https://www.alchemists.io/articles/ruby_pattern_matching

  188. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  189. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  190. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  191. 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! https://www.alchemists.io/articles/ruby_pattern_matching
  192. 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! https://www.alchemists.io/articles/ruby_pattern_matching
  193. 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! https://www.alchemists.io/articles/ruby_pattern_matching
  194. Classes https://www.alchemists.io/articles/ruby_pattern_matching

  195. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  196. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  197. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  198. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  199. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  200. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  201. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  202. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  203. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  204. Structs https://www.alchemists.io/articles/ruby_pattern_matching

  205. Structs Point = Struct.new :x, :y https://www.alchemists.io/articles/ruby_pattern_matching

  206. case Point[1, 2] in Point[..5, ..5] then "matched" else "unmatched"

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

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

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

    end "matched" Structs Point = Struct.new :x, :y Point[..5, ..5] https://www.alchemists.io/articles/ruby_pattern_matching
  210. 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] https://www.alchemists.io/articles/ruby_pattern_matching
  211. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  212. 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" https://www.alchemists.io/articles/ruby_pattern_matching
  213. 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 https://www.alchemists.io/articles/ruby_pattern_matching
  214. 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" https://www.alchemists.io/articles/ruby_pattern_matching
  215. 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" https://www.alchemists.io/articles/ruby_pattern_matching
  216. 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" https://www.alchemists.io/articles/ruby_pattern_matching
  217. Structs Point = Struct.new :x, :y https://www.alchemists.io/articles/ruby_pattern_matching

  218. case Point[1, 2] in Point(x:, y:) then "matched" else "unmatched"

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

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

    end "matched" Structs Point = Struct.new :x, :y https://www.alchemists.io/articles/ruby_pattern_matching
  221. https://www.alchemists.io/projects

  222. Examples https://www.alchemists.io/projects

  223. https://www.alchemists.io/projects/rubysmith

  224. Rubysmith https://www.alchemists.io/projects/rubysmith

  225. https://www.alchemists.io/projects/rubysmith Rubysmith

  226. https://www.alchemists.io/projects/rubysmith Rubysmith

  227. https://www.alchemists.io/projects/rubysmith Rubysmith

  228. https://www.alchemists.io/projects/rubysmith 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
  229. https://www.alchemists.io/projects/rubysmith 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
  230. https://www.alchemists.io/projects/rubysmith 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
  231. https://www.alchemists.io/projects/rubysmith 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
  232. https://www.alchemists.io/projects/rubysmith 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
  233. https://www.alchemists.io/projects/rubysmith 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
  234. https://www.alchemists.io/projects/rubysmith 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
  235. https://www.alchemists.io/talks/janus

  236. Janus https://www.alchemists.io/talks/janus

  237. Janus 🎉 Open Sourced (future) https://www.alchemists.io/talks/janus

  238. Janus https://www.alchemists.io/talks/janus

  239. Janus https://www.alchemists.io/talks/janus

  240. 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 https://www.alchemists.io/talks/janus
  241. 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 https://www.alchemists.io/talks/janus
  242. 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 https://www.alchemists.io/talks/janus
  243. 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 https://www.alchemists.io/talks/janus
  244. 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 https://www.alchemists.io/talks/janus
  245. 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 https://www.alchemists.io/talks/janus
  246. 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 https://www.alchemists.io/talks/janus
  247. 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 https://www.alchemists.io/talks/janus
  248. None
  249. Guidelines 💡

  250. Guidelines https://www.alchemists.io/articles/ruby_pattern_matching

  251. Guidelines • Use sorted arrays. https://www.alchemists.io/articles/ruby_pattern_matching

  252. Guidelines • Use sorted arrays. • Use limited fi nds:

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

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

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

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

  257. https://www.alchemists.io 🙇 Thanks