Ruby Pattern Matching

Ruby Pattern Matching

Learn to leverage the power for Ruby Pattern Matching for Command Line Interfaces (CLIs), dealing with Application Programming Interfaces (APIs), and much more!

E0e036f89c14b3e59640318eedf9670b?s=128

Brooke Kuhlmann

October 02, 2020
Tweet

Transcript

  1. Ruby Pattern Matching Presented by Brooke Kuhlmann

  2. Ruby 2.7.0 History https://rubyreferences.github.io/rubychanges/2.7.html

  3. Ruby 2.7.0 Warning[:experimental] = false History https://rubyreferences.github.io/rubychanges/2.7.html

  4. Basic Syntax <expression> in <pattern> https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html

  5. Basic Syntax <expression> in <pattern> https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html Standalone

  6. Basic Syntax <expression> in <pattern> https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html Standalone

  7. Basic Syntax case <expression> in <pattern1> then ... in <pattern2>

    then ... in <pattern3> then ... else ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
  8. Basic Syntax case <expression> in <pattern1> then ... in <pattern2>

    then ... in <pattern3> then ... else ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html C ase
  9. Basic Syntax case <expression> in <pattern1> then ... in <pattern2>

    then ... in <pattern3> then ... else ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html C ase
  10. Invalid Syntax case <expression> in <pattern1> then ... when <pattern2>

    then ... else ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
  11. Invalid Syntax case <expression> in <pattern1> then ... when <pattern2>

    then ... else ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
  12. Invalid Syntax https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html

  13. Exhaustive Matching case <expression> in <pattern1> then ... in <pattern2>

    then ... in <pattern3> then ... end https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
  14. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html NoMatchingPatternError

  15. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html NoMatchingPatternError ♥

  16. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html NoMatchingPatternError ♥

  17. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html Control Flow Pattern Matching

  18. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html case <expression> when <pattern1> then ... when

    <pattern2> then ... when <pattern3> then ... end case <expression> in <pattern1> then ... in <pattern2> then ... in <pattern3> then ... end Control Flow Pattern Matching
  19. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html case <expression> when <pattern1> then ... when

    <pattern2> then ... when <pattern3> then ... end case <expression> in <pattern1> then ... in <pattern2> then ... in <pattern3> then ... end Control Flow Pattern Matching Nothing Happens
  20. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html case <expression> when <pattern1> then ... when

    <pattern2> then ... when <pattern3> then ... end case <expression> in <pattern1> then ... in <pattern2> then ... in <pattern3> then ... end Control Flow Pattern Matching Nothing Happens NoMatchingPatternError
  21. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html case <expression> when <pattern1> then ... when

    <pattern2> then ... when <pattern3> then ... end case <expression> in <pattern1> then ... in <pattern2> then ... in <pattern3> then ... end Control Flow Pattern Matching
  22. Exhaustive Matching https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html case <expression> when <pattern1> then ... when

    <pattern2> then ... when <pattern3> then ... end case <expression> in <pattern1> then ... in <pattern2> then ... in <pattern3> then ... end Control Flow Pattern Matching Style/MissingElse: Enabled: true
  23. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
  24. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html ===
  25. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html === [<subpattern>, <subpattern>, <subpattern>, ...]
  26. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html === [<subpattern>, <subpattern>, <subpattern>, ...] {key: <subpattern>, key: <subpattern>, ...}
  27. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html === [<subpattern>, <subpattern>, <subpattern>, ...] {key: <subpattern>, key: <subpattern>, ...} _
  28. Supported Patterns • Case Equality: • Arrays: • Hashes: •

    Glob: • Combination: https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html === [<subpattern>, <subpattern>, <subpattern>, ...] {key: <subpattern>, key: <subpattern>, ...} _ |
  29. Non-Primitive Interface https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html

  30. Non-Primitive Interface https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html class Point attr_reader :x, :y def initialize

    x, y @x = x @y = y end def deconstruct [x, y] end def deconstruct_keys keys {x: x, y: y} end end
  31. Non-Primitive Interface https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html class Point attr_reader :x, :y def initialize

    x, y @x = x @y = y end def deconstruct [x, y] end def deconstruct_keys keys {x: x, y: y} end end Array
  32. Non-Primitive Interface https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html class Point attr_reader :x, :y def initialize

    x, y @x = x @y = y end def deconstruct [x, y] end def deconstruct_keys keys {x: x, y: y} end end Array Hash
  33. https://gitlab.com/procps-ng/procps

  34. Demo https://gitlab.com/procps-ng/procps

  35. brew install watch Demo https://gitlab.com/procps-ng/procps

  36. brew install watch Demo https://gitlab.com/procps-ng/procps watch --interval 1 --color --beep

    --exec ruby patterns.rb
  37. https://www.alchemists.io

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