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 Slide

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

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

  4. What

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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!

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

  13. 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 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: }
    The Joy of Pattern Matching!

    View 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 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 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
    [*, , *]
    The Joy of Pattern Matching!
    _

    View 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
    The Joy of Pattern Matching!
    _

    View 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 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
    |
    The Joy of Pattern Matching!
    _

    View 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
    The Joy of Pattern Matching!
    _

    View Slide

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

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

    View Slide

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

    View Slide

  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

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

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

    View 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, \*]).
    https://www.alchemists.io/articles/ruby_pattern_matching

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

    View Slide

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

  31. Syntax: Standalone

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

  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

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

  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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

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

  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

    View Slide

  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

    View Slide

  52. Syntax: Case

    View Slide

  53. Syntax: Case
    case


    in then ...


    in then ...


    in then ...


    else ...


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

    View Slide

  54. Syntax: Case
    case


    in then ...


    in then ...


    in then ...


    else ...


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

    View Slide

  55. case


    in then ...


    when then ...


    else ...


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

    View Slide

  56. case


    in then ...


    when then ...


    else ...


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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  60. NoMatchingPatternError

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

    View Slide

  61. NoMatchingPatternError

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

    View Slide

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

    View Slide

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

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

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

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

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

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

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

    View Slide

  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

    View Slide

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

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

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

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

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

  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

    View Slide

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

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

  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

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

  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

    View Slide

  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

    View Slide

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

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

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

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

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

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

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

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

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

    View Slide

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

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

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

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  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

    View Slide

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

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

  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

    View Slide

  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

    View Slide

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

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  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

    View Slide

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

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

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

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

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

    View Slide

  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

    View Slide

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

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

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

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

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

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

    View Slide

  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

    View Slide

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

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

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

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

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

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

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

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

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

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

    View Slide

  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

    View Slide

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

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

  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

    View Slide

  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

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

    View Slide

  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

    View Slide

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

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

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

    View Slide

  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

    View Slide

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

    View Slide

  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

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

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

    View Slide

  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

    View Slide

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

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

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

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

  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

    View Slide

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

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

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

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

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

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

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

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

  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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

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

    View Slide

  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

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

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

    View Slide

  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

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

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

    View Slide

  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

    View Slide

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

    View Slide

  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

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

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

  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

    View Slide

  248. View Slide

  249. Guidelines
    💡

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

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

    View Slide