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

Pattern Matching in Ruby ( 2017 RubyKaigi)

やきとりい
September 20, 2017

Pattern Matching in Ruby ( 2017 RubyKaigi)

This presentation is about the proposal of new feature of Ruby: pattern matching.
It was published at the session of RubyKaigi 2017
http://rubykaigi.org/2017/presentations/yotii23.html

やきとりい

September 20, 2017
Tweet

More Decks by やきとりい

Other Decks in Programming

Transcript

  1. Pattern Matching
    in Ruby Yuki Torii, Sep.20.2017

    View full-size slide

  2. Who am I?
    ❖ a Rails Developer of Everyleaf
    Corporation.
    ❖ a Japanese translator of
    ❖ “Hello Ruby” series

    (By Linda Liucusʣ
    ❖ “Programing Elixir” 

    (By Dave Thomasʣ
    corporating with Koich
    Sasada
    ❖ a member of Rails Girls in JP

    View full-size slide

  3. The Goal of This Presentation
    ❖ Main: You want to use Pattern Matching in Ruby
    ❖ Sub: You know how to extend Ruby by Ruby

    View full-size slide

  4. Agenda
    ❖ Demo 1
    ❖ What is Pattern Matching
    ❖ The Detail of My Proposal
    ❖ Demo 2 (details)
    ❖ Implementation by (almost) Ruby 

    as a Conceptual Model
    ❖ Future Issues

    View full-size slide

  5. First Demo(in live)
    Code
    Result

    View full-size slide

  6. What is Pattern Matching
    “pattern matching is the act of checking a given
    sequence of tokens for the presence of the
    constituents of some pattern.”
    – WikiPedia Pattern matching

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

    View full-size slide

  7. What is Pattern Matching
    ❖ ex. Ruby’s Regexp is also Pattern Matching
    1BUUFSO 4FRVFODFPG

    5PLFOT

    View full-size slide

  8. What is Pattern Matching
    ❖ ex2. Elixir’s Pattern Matching
    ❖ Elixir is a functional programing language working
    on Erlang VM.
    ❖ Elixir’s grammar is influenced by Ruby
    ❖ It has powerful pattern matching system

    View full-size slide

  9. What is Pattern Matching
    ❖ In Elixir, `=` is always Pattern Matching, 

    not assigning value

    View full-size slide

  10. What is Pattern Matching
    ❖ More examples of Elixir

    View full-size slide

  11. What is Pattern Matching
    ❖ Elixir has guards: more complex check.
    result#=>
    < guard
    clause

    View full-size slide

  12. What is Pattern Matching
    ❖ Ruby’s Existing Pattern Matching gem “pattern-
    match”

    https://github.com/k-tsj/pattern-match

    View full-size slide

  13. The detail of my proposal
    ❖ %p
    ❖ like %w, %i
    ❖ `=~` for matching
    Basic Usage

    View full-size slide

  14. The detail of my proposal
    ❖ `===` is a alias of `=~`

    View full-size slide

  15. Demo of details

    View full-size slide

  16. demo(with case when statement)
    Code
    Result

    View full-size slide

  17. demo(with hash, and _(ignored pattern))
    Code
    Result

    View full-size slide

  18. demo(nested pattern)
    Code
    Result

    View full-size slide

  19. demo(with regexp)
    Code
    Result

    View full-size slide

  20. demo(with Class of object)
    Code
    Result

    View full-size slide

  21. Implementation as a concept model
    ❖ I wrote almost part of pm by Ruby.

    C-lang diff is only below:
    ❖ (show parse.y & complile.c diff )
    ❖ What trick did I use? compile.c
    parse.y

    View full-size slide

  22. Implementation as a concept model
    Ruby script
    Parse
    Compile
    Ruby byte code Evaluator

    View full-size slide

  23. Implementation of the concept model
    Ruby script
    Parse
    Compile
    Ruby byte code
    PatternMatching
    %p([a, 1]) =~ [3, 1]
    “[a, 1]”
    variables list [“a”]
    Define variabies
    Evaluator
    pattern_match obj
    Parse pattern
    Convert script,

    Generate inspections with
    the additional code

    View full-size slide

  24. How I implement it as concept model
    Ruby script
    Parse
    Compile
    Ruby byte code
    PatternMatching
    %p([a, 1]) =~ [3, 1]
    “[a, 1]”
    variables list
    Define variabies
    Evaluator
    pattern_match obj
    Parse pattern
    build AST
    check matching
    assign values
    variables list [“a”]
    Convert script,

    Generate inspections with
    the additional code
    Convert Script ?

    View full-size slide

  25. Implementation of the concept model
    %p([a, 1]) =~ [3, 1]
    in Compiling to byte code
    Convert Script
    PatternMatch.save_binding(b)

    %p([a, 1]) =~ [3, 1]
    Ruby byte code
    save binding
    build AST
    check matching
    assign values
    build AST
    check matching
    assign values

    View full-size slide

  26. Implementation of the concept model
    ❖ Ruby library
    ❖ Parse patterns
    ❖ Matching values
    ❖ Comparing
    ❖ Assignment

    View full-size slide

  27. Back to the goals
    ❖ Main: you want to use Pattern Matching in Ruby
    ❖ Did it attract you?
    ❖ Sub: you know How to extend Ruby by Ruby
    ❖ Now you know the backdoor.

    View full-size slide

  28. Special Thanks
    ❖ Koichi Sasada
    ❖ My great adviser of Ruby’s implementation.
    ❖ Tsujimoto-san
    ❖ the author of pattern-match gem.

    View full-size slide

  29. Future Issues
    ❖ Syntax. Is %p the best for making pattern?
    ❖ Specification of Guard clause
    ❖ Improve the parser.
    ❖ https://github.com/yakitorii/pattern-match-ruby

    View full-size slide