Slide 1

Slide 1 text

Pattern Matching in Ruby Yuki Torii, Sep.20.2017

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

First Demo(in live) Code Result

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

What is Pattern Matching ❖ In Elixir, `=` is always Pattern Matching, 
 not assigning value

Slide 11

Slide 11 text

What is Pattern Matching ❖ More examples of Elixir

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

What is Pattern Matching ❖ Ruby’s Existing Pattern Matching gem “pattern- match”
 https://github.com/k-tsj/pattern-match

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Demo of details

Slide 17

Slide 17 text

demo(with case when statement) Code Result

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

demo(nested pattern) Code Result

Slide 20

Slide 20 text

demo(with regexp) Code Result

Slide 21

Slide 21 text

demo(with Class of object) Code Result

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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 ?

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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