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. 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
  2. The Goal of This Presentation ❖ Main: You want to

    use Pattern Matching in Ruby ❖ Sub: You know how to extend Ruby by Ruby
  3. 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
  4. 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
  5. What is Pattern Matching ❖ ex. Ruby’s Regexp is also

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

    Pattern Matching, 
 not assigning value
  8. What is Pattern Matching ❖ Ruby’s Existing Pattern Matching gem

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

    %i ❖ `=~` for matching Basic Usage
  10. 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
  11. 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
  12. 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 ?
  13. 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
  14. Implementation of the concept model ❖ Ruby library ❖ Parse

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

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

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