Pattern Matching

Summarized using AI

Pattern Matching in Ruby

YUKI TORII • September 18, 2017 • Hiroshima, Japan

The video titled "Pattern Matching in Ruby" is presented by Yuki Torii at RubyKaigi 2017. In this session, Yuki discusses the concept of pattern matching in Ruby, outlining its potential to enhance the language's expressiveness and usability. The presentation aims to propose a specification for pattern matching in Ruby along with an implementation that Yuki developed.

Key Points Discussed:
- Introduction to Yuki Torii: Yuki is a developer at Every Leaf Cooperation and has significant experience in translation and programming literature. He is also a member of RubyKaigi Japan.
- Purpose of Presentation: The goal is to explain pattern matching, demonstrate its implications for Ruby, and showcase an implementation designed to extend Ruby's functionality.
- Understanding Pattern Matching: Yuki provides a fundamental explanation of pattern matching, including its definition from Wikipedia, and refers to existing pattern matching capabilities in Ruby, including regular expressions.
- Comparison with Elixir: The presentation contrasts Ruby’s current matching capabilities with Elixir's advanced pattern matching system, highlighting how Elixir allows complex matches and error handling that can improve usability.
- Proposed Improvements: Yuki introduces a new operator %p for Ruby, which would mimic Elixir's syntax and enable clearer and more elegant pattern matching.
- Practical Examples: The speaker shares simple and complex examples using Ruby code, demonstrating how the proposed pattern matching features work, including matching arrays and creating hash patterns to assign variable names based on matches.
- Vision for Ruby: The proposal emphasizes making Ruby more user-friendly and powerful in handling complex data structures.
- Conclusion and Call for Feedback: Yuki concludes by encouraging listeners to consider the value of the proposed features and invites questions from the audience, thanking his husband for his support throughout this process.

This presentation underlines a significant potential enhancement for Ruby, aimed at enhancing its utility and elegance by adopting more expressive pattern matching capabilities.

Pattern Matching in Ruby
YUKI TORII • September 18, 2017 • Hiroshima, Japan

RubyKaigi2017
http://rubykaigi.org/2017/presentations/yotii23.html

Do you want pattern matching in Ruby? I want. It will make Ruby more elegant, more useful, and more comfortable. So in this talk, I'll propose a specification about pattern matching in Ruby and will show my implementation in parse.y (a part of them).

RubyKaigi 2017

00:00:11 I have translated the 'Ruby' series by Linda Lucas into Japanese. This series is a picture book about programming and computers for children. Additionally, I have translated 'Programming Elixir' by Dave Thomas with my husband, Koichi. I am also a member of RubyKaigi Japan.
00:01:02 This is my family. These are my kids and my husband. Unfortunately, I cannot talk about my family for 40 minutes today, as I have a goal for this presentation.
00:01:19 The main goal is to discuss pattern matching in Ruby, and the sub-goal is to show you how to extend Ruby by leveraging my pattern matching implementation. I wrote this with only several lines of Sheepsh language and mostly in Ruby. I will introduce how I did this.
00:02:02 First, I chose my pattern matching design, and next I will talk about what pattern matching is. I will show the details of my proposals, introduce my implementation, and discuss future issues.
00:02:51 Can you see the code here? Okay, let’s look into this code. You know, in Ruby, it makes sense when matching is done with patterns. For instance, when matching with an array, the first element is 'OK' and the second is a variable.
00:03:17 Let's say on the right side we have an object that matches the array. If the first element is 'OK' and the second element is 200, it will match and assign these values accordingly.
00:04:49 Let me show a simple example in Ruby. As you can see, it works perfectly. Before diving into the details, I would like to explain what pattern matching is.
00:05:56 I read on Wikipedia that pattern matching is the act of checking a given sequence of tokens against a pattern. However, I found it hard to understand, because it is quite broad, so I looked for examples.
00:06:41 Ruby does support pattern matching with regular expressions. For example, a pattern can begin with a capital 'C' and end with a lowercase 'p'. This means the string must match this condition.
00:07:14 Next, I want to talk about Elixir, which is a functional programming language that has a very powerful pattern matching system. Elixir always uses pattern matching, which is more than just assignment. In Elixir, you can do more complex checks.
00:09:09 For example, if you attempt to match with variables, the left side must comply with the pattern on the right side. If they don't match, an error occurs. This allows you to impose complex conditions directly.
00:10:18 I find this pattern matching system of Elixir quite fascinating, and I want to incorporate similar features into Ruby. The Ruby system is great, but I feel like it could be enhanced to be more intuitive and simple.
00:11:14 So here is my proposal. I suggest an operator like '%p', which works similarly to pattern matching in Elixir. It allows for simple and elegant syntax for users.
00:12:23 This is a simple example of how this can be implemented. You can match an array where the first element is 'NZ' and the second is a variable. Then, it checks different patterns and you can see it works successfully.
00:13:32 Now, I want to share more complex patterns. By using hash patterns, you can assign variable names obtained from matches. For example, if the name is from a specific hash, it can match and extract values easily.
00:15:00 These examples showcase how powerful the pattern matching feature can be. Furthermore, it can work with regular expressions to extend the possibilities of how data can be matched and processed.
00:17:14 In summary, this proposal for pattern matching aims to make Ruby more expressive and user-friendly in handling complex data structures. I believe it is an attractive addition to Ruby.
00:18:01 Thank you all for listening. I hope you see the value in this feature for Ruby. Special thanks to my husband, Koichi Sasada, who has been an excellent advisor in this journey.
00:20:27 And lastly, if you have any questions, please feel free to ask. I'm excited to hear your opinions and thoughts about these enhancements to Ruby.
00:21:19 Thank you for the great talk. I think we have a few minutes for Q&A. Please feel free to ask anything you'd like.
Explore all talks recorded at RubyKaigi 2017
+63