Hello. Good morning. So yeah, as you can see, I'll be talking about pattern matching. But before I get into it Let me introduce myself. My name is quite long But I usually go by just take Currently I'm from here from Bangkok and I'm currently living in Tokyo I used Ruby on Rails and Vue.js on a daily basis and around two years ago. I Found out about this functional language called elixir Yeah, it's I think it was really like hyped up in the Ruby community So I checked it out and I found out about this feature called pattern matching And yeah, I I felt it was really neat and elixir and you know, I really liked it and yeah basically You know what pattern matching is is that it deconstruct complex data structure it buying You know values from those complex data structure to variables It allows elixir, which is a dynamically typed language to have method overloading Sometimes when you look at Java you you say, you know, it's a typed language So you can differentiate between each methods through, you know, the argument types But you can't do that with Ruby because Ruby is not typed but with elixir. They they did it with pattern matching So yeah, I think that's really cool. And you know to to show you some example I'll show you pattern matching in elixir first This is pattern matching in elixir. It's exactly like multiple assignment in Ruby Yeah, so a is binded to hello, B is binded to world and C is binded to 42 and It can also, you know deconstruct an array into head and tail where head is the first element and tail is the rest of the array Yeah But it does way more than that. It also checks for values. So for an example here It's pretty much the same but the last instead of a variable we have 42 and What this does is it checks it binds a to hello B to world and then it checks that the last element 42 are the same So if you look at the example here 88 that's not 42 So, you know, the last Value is not the same it raises an error instead of binding the variables Also check for types and stuff like that It also, you know, you can also do Match pattern matching with maps. This is kind of like hash in Ruby. So we have animal As the key and then the value variable animal and then on the right-hand side We have dog as the value. So, you know, wait, sorry That should be animal and stuff BTC and that'll print out dog. Sorry. I last minute changed that slide Over here and then with this we can do We can define multiple Definition for process and then we take in different maps with different keys and depending on the keys Different process method gets cold. I think there's a really clean way to you know, differentiate between logic between each data structure. So, yeah, and Basically what pattern matching is it is that it explicitly tells you what data is to expect It assigned vero value to a variable in those complex data structure. It also allow elixir to have method overloading Basically think of pattern matching as something like if else statement with, you know Paul powerful variable assignment And yeah, you might be wondering why I'm talking about this because it's a functional programming thing well in in ruby 2.7 They're Implementing pattern matching Yeah, and you know, it's still experimental if you download the newest version of ruby development The trunk you can try this out Every time you run the pattern matching code it says don't run this in production. It's still experimental Yeah, and you know, I tried it out and I'll tell you guys, you know more about it So first I guess let's talk about the syntax In ruby use case statement to do pattern matching So you have a case statement and you provide it with an expression or a variable and then Instead of the usual when you use in and then you provide a Pattern in this and these clauses it also supports if and unless Clauses and then if nothing else match you can do else as a default Block So that's you know very similar to a case statement So I'll show you an example but first let's look at this Problem let's say we have An array of translation where you know the there's a Original language original text translated language and translate translated text as the value in the array and You know, we want to check that the original language is Thai and the translated language is English And then we print out the translations what I would do with normal ruby is I would multiple assign The very the array into all these variables check that the Original language and the translated language is correct and then I'll print out the translation But with pattern matching You could do it with this code here So instead of having to multiple assign and check, you know that the values are correct You can just tell the program that you know you want the first array to be th You want the third array to be en it's you know a lot more expressive in my opinion And yeah, you can bind the original text and translated text to the second and the fourth variables in that in the array so what it does is it checks that the length of the arrays are the same and Then it checks that the value for each index are the same and then if it's a variable it binds the variable Yeah, yeah, and this is a comparison between you know with pattern matching and without pattern matching Yeah, instead of having to go into the data structure and check each value You can just you know explicitly tell it that this is the kind of array you want and which variable you want to bind So that was pattern matching with array This is also exactly the same problem, but instead of array we have hash Yeah original language translated language original text translated text exactly the same and what I would do is I would check This is with you know, no more Ruby code at the moment I would check that you know each value in the in the hash has the value. I want to I want to you know check and I Would check that you know the key in the hash also exists and Then I would print out the translations. So, you know, that's Ruby, you know you Nothing new but with pattern matching. I I can write it as such So the translation variable is you know the same as before but instead of going into the data structure and check All these values you can tell the program exactly how you want how you want your hash to look like and Then which you know Value to bind and then which value to use basically what it does is it checks that the key in the pattern exists in the expression It checks that the value for each keys matches and Then it binds The variable to you know the value You know, you can also rewrite, you know, you see this original text to original text translated text translated text There's a syntactic sugar where you can just do that, you know have a Value and then colon instead of rewriting it twice So this is the comparison with pattern matching and without pattern matching Yeah, so very similar to the array example I think instead of having to go into the data structure checking all the values you can, you know Write out the hash you want and it tells you exactly, you know What to expect? Yeah, so that's pattern matching with hash and array There are also other tools like the pin operator, this is exactly the same as the one in elixir It evaluates variables instead of binding the variables Yeah, well in this example here, we have get translations where we take an original language translated language and translation array and then we pattern match with translation array and Instead of rebinding, you know original language and translated lang we put a pin operator in front of these variables and what it does is it It'll evaluate these variables instead of binding it So yeah, the first example we have th and en and in the array. We also have th and en So that will you know print out the translation, but with the second example, we have j a instead of En and that will raise No pattern match error Yeah There's also the ignore operator where you know What it does is basically you tell the program that you don't care about this value So over here, you know both matches even though the third element are different and that's because you know It's an ignore operator We also have they also have other operators like a sign operator where you know, it would bind values to a variable So In this example here You have translation hash and then you match translation hash with you know original key and translation key and Inside you also check for lang that it's th and en and then you assign this to original hash and translation hash So what we do is we check that the lang is correct But when we print out these values the text is also in the variable and that's because you assign The whole object into these variables Yeah, so those are you know some tools Ruby pattern matching in 2.7 has They also have a new special methods So this is something like initialize where you know initialize you you do dot new and initialize get called with deconstructing deconstruct keys you can define these method in your Classes and then when you you know pattern match against these the instance of these objects Deconstructing deconstruct keys get called so let me give you an example Just to make it clear so we have a class called note With deconstruct define and deconstruct keys define and then on the left-hand side on the right-hand side We have you know this initialize of Initialization of values, and then we pass the note to a pattern matching class, and then we match it against an array Yeah, if we match against and against an array deconstruct methods get called and then it'll matches the value the result of deconstruct against the pattern and it's the same with deconstruct keys, but just with hash and With deconstruct method you have to return an array or it'll give you an error and with deconstruct keys You have to return a hash or it'll give you an error. So that's you know type matching in type type checking Ruby. That's something new So let's let's I want to So yeah, that's that's what I found out about pattern matching in Ruby I want to give you you know what I think about this whole thing and I want to end on a good note So let's start with the bad stuff first having coming Having coming from elixir like the first time I use pattern matching was in elixir. I feel like This is not exactly what I expect you know having pattern matching in a switch statement The syntax feels a little clunky to match just one pattern you need at least three lines of code because it's a It's a switch statement and if you want to match multiple patterns you would have a big you know a very big case statement And I'm so also not too sure about the performance at the moment I did benchmarking and it's around twice the speed of It's it's twice the time to execute the pattern matching Code against you know just hash lookups But it's still experimental and I see that you know each time you call pattern matching You know all the deconstruct and deconstruct keys method get called each time So you know that's something to be improved and I think it'll be better, but I'm still not too sure With the pros I feel like you know it has the potential to make Ruby a little easier to read Yeah, and I like that you know There's an effort being made to bring something cool from other languages to Ruby like you know in elixir a lot of people like pattern matching and you know, that's why they're trying to implement it in Ruby and Yeah, I'm really keen to see how how pattern matching will go from here. Yeah, and That's all I have. Thanks