Pattern Matching in Ruby ( 2017 RubyKaigi)

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

C67596c92e9d161d314f69de3f05f00c?s=128

やきとりい

September 20, 2017
Tweet

Transcript

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

  2. Who am I?<Yuki Torii> ❖ 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
  3. None
  4. The Goal of This Presentation ❖ Main: You want to

    use Pattern Matching in Ruby ❖ Sub: You know how to extend Ruby by Ruby
  5. 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
  6. First Demo(in live) Code Result

  7. 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
  8. What is Pattern Matching ❖ ex. Ruby’s Regexp is also

    Pattern Matching 1BUUFSO 4FRVFODFPG
 5PLFOT
  9. 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
  10. What is Pattern Matching ❖ In Elixir, `=` is always

    Pattern Matching, 
 not assigning value
  11. What is Pattern Matching ❖ More examples of Elixir

  12. What is Pattern Matching ❖ Elixir has guards: more complex

    check. result#=> < guard clause
  13. What is Pattern Matching ❖ Ruby’s Existing Pattern Matching gem

    “pattern- match”
 https://github.com/k-tsj/pattern-match
  14. The detail of my proposal ❖ %p ❖ like %w,

    %i ❖ `=~` for matching Basic Usage
  15. The detail of my proposal ❖ `===` is a alias

    of `=~`
  16. Demo of details

  17. demo(with case when statement) Code Result

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

  19. demo(nested pattern) Code Result

  20. demo(with regexp) Code Result

  21. demo(with Class of object) Code Result

  22. 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
  23. Implementation as a concept model Ruby script Parse Compile Ruby

    byte code Evaluator
  24. 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
  25. 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 ?
  26. 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
  27. Implementation of the concept model ❖ Ruby library ❖ Parse

    patterns ❖ Matching values ❖ Comparing ❖ Assignment
  28. 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.
  29. Special Thanks ❖ Koichi Sasada ❖ My great adviser of

    Ruby’s implementation. ❖ Tsujimoto-san ❖ the author of pattern-match gem.
  30. Future Issues ❖ Syntax. Is %p the best for making

    pattern? ❖ Specification of Guard clause ❖ Improve the parser. ❖ https://github.com/yakitorii/pattern-match-ruby