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

December 20, 2022
Tweet

More Decks by Brooke Kuhlmann

Other Decks in Programming

Transcript

  1. Ruby Pattern Matching
    Presented by Brooke Kuhlmann

    View full-size slide

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

    View full-size slide

  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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. https://www.alchemists.io/articles/ruby_pattern_matching
    Overview

    View full-size slide

  10. 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!

    View full-size slide

  11. 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!

    View full-size slide

  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!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. History
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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).
    • Find patterns (i.e. in [*, query, \*]).
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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. in [*, query, \*]).
    • Ruby 3.1.0: Rightward assignment and boolean checks became of
    fi
    cially supported.
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  30. Syntax: Standalone

    View full-size slide

  31. Syntax: Standalone
    in
    =>
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  32. Syntax: Standalone
    in
    =>
    💡 True || False
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  33. Syntax: Standalone
    in
    =>
    💡 True || False
    💡 Assignment
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  34. Syntax: Standalone (in)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  43. Syntax: Standalone (=>)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  44. {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

    View full-size slide

  45. {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

    View full-size slide

  46. {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

    View full-size slide

  47. {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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  50. {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

    View full-size slide

  51. Syntax: Case

    View full-size slide

  52. Syntax: Case
    case


    in then ...


    in then ...


    in then ...


    else ...


    end
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  53. Syntax: Case
    case


    in then ...


    in then ...


    in then ...


    else ...


    end
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  54. case


    in then ...


    when then ...


    else ...


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

    View full-size slide

  55. case


    in then ...


    when then ...


    else ...


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

    View full-size slide

  56. Syntax: Case
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  57. c
    ccc c cc c
    c
    c
    c ccccc
    e
    ee
    Syntax: Case (Exhaustive Matching)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  59. NoMatchingPatternError

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

    View full-size slide

  60. NoMatchingPatternError

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

    View full-size slide

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

    View full-size slide

  62. case


    when then ...


    when then ...


    when then ...


    end
    case


    in then ...


    in then ...


    in then ...


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

    View full-size slide

  63. case


    when then ...


    when then ...


    when then ...


    end
    case


    in then ...


    in then ...


    in then ...


    end
    Control Flow Pattern Matching
    Nothing Happens 😢
    Syntax: Case (Exhaustive Matching)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  64. case


    when then ...


    when then ...


    when then ...


    end
    case


    in then ...


    in then ...


    in then ...


    end
    Control Flow Pattern Matching
    Nothing Happens 😢 NoMatchingPatternError 🎉
    Syntax: Case (Exhaustive Matching)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  65. case


    when then ...


    when then ...


    when then ...


    end
    case


    in then ...


    in then ...


    in then ...


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

    View full-size slide

  66. case


    when then ...


    when then ...


    when then ...


    end
    case


    in then ...


    in then ...


    in then ...


    end
    Control Flow Pattern Matching
    Style/MissingElse:
    Enabled: true
    Syntax: Case (Exhaustive Matching)
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  67. Syntax: Optional
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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
    ⚠ Required for standalone syntax
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  72. Whole Versus Partial
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    "unmatched"
    Array Hash
    Whole Versus Partial
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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
    💡
    Order
    Matters
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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" "matched"
    Array Hash
    Whole Versus Partial
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  79. Caveat: Empty Hashes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  85. Finds
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  91. Finds
    ⚠ Expensive
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  94. 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
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    Finds
    ⚠ Expensive
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  96. Voids
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    "matched whole hash"
    Hashes Only
    Voids
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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
    ⚠ Pattern end only
    Voids
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    Voids

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

    View full-size slide

  102. Remainders
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    "matched"
    Array Hash
    Remainders
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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" "matched"
    Array Hash
    Remainders
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  108. Variable Binding
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    "matched: 1"
    Array
    Variable Binding
    Hash
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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"
    "matched: 1"
    Array
    Variable Binding
    Hash
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  114. Variable Binding: Simpli
    fi
    ed
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    "matched: 1"
    Array
    Hash
    Variable Binding: Simpli
    fi
    ed
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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"
    "matched: 1"
    Array
    Hash
    Variable Binding: Simpli
    fi
    ed
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  120. Variable Binding: Hash Only
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  124. Explicit Brackets
    Variable Binding: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    "matched: Apple"
    "matched: Apple"
    Array
    Hash
    Explicit Brackets
    Variable Binding: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  128. Variable Binding: Remainder
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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
    "matched: 1, [2, 3]"
    Array
    Hash
    Variable Binding: Remainder
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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, {:b=>2, :c=>3}"
    "matched: 1, [2, 3]"
    Array
    Hash
    Variable Binding: Remainder
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  136. Variable Pinning
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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
    "matched: 1"
    Variable Pinning
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    `expectation` is pinned to `1`
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    "unmatched: 5"
    Variable Pinning
    `expectation` is pinned to `1`
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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`
    `expectation` is pinned to `5`
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  145. Variable Pinning: Comparison
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    "values are identical"
    Variable Pinning: Comparison
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    "values are different"
    Variable Pinning: Comparison
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    1
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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 1
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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
    1 1
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  153. Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    "matched: 2"
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    Key
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    Pin
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    Value
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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"

    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    "unmatched"
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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"
    Pin ("high")
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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")
    "middle" != "high"
    Variable Pinning: Nested
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  165. Variable Pinning: Expression
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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
    "matched: 4"
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  174. Combinations
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    "matched"
    Array
    Hash
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    "matched"
    Array
    Hash
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  179. Combinations
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    matched
    🚫 🚫
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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
    🚫 🚫
    Meant to signify discarded values only!
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  186. Guards
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    "matched"
    "matched"
    Array
    Hash
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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
    💡`unless` works too!
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  193. Classes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    Array
    Classes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    Hash
    Classes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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
    # => "matched: 1"
    Array Pattern
    Classes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  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"
    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

    View full-size slide

  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
    # => "matched: 1"
    Array Pattern
    Hash Pattern
    Classes
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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

    View full-size slide

  203. Structs
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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
    "matched"
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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"
    Same Type
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

  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"
    "unmatched"
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  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
    Wrong Type
    "matched"
    "unmatched"
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  220. https://www.alchemists.io/projects

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  234. https://www.alchemists.io/talks/janus

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  247. Guidelines
    💡

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  253. Guidelines
    • Use sorted arrays.
    • Use limited
    fi
    nds: [*, , *].
    • Avoid unnecessary deconstruction.
    • Avoid shadow variables.
    • Avoid complicated nested patterns.
    https://www.alchemists.io/articles/ruby_pattern_matching

    View full-size slide

  254. https://www.alchemists.io
    Thanks

    View full-size slide

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

    View full-size slide