External Publication
Visit Post

[ruby-talk:444825] Pattern matching as String, passing patters to functions

Ruby Mailing List Mirror [Unofficial] June 1, 2026
Source

String keys do not work with pattern matching, or more specifically pattern expressions in this case. The only way to get around that is to use something to symbolize the keys.

That said, I have argued before that we should treat pattern expression keys as irrelevant, whether they are Strings or Symbols.

baweaver

The Case for Pattern Matching Key Irreverence in Ruby

I had alluded to this post a bit earlier on Twitter, and in the Ruby bug tracker, but wanted to more...

The other option is to use something like Dio to make it irreverent for you:

github.com

GitHub - baweaver/dio: Dio - Dive Into Objects - Ruby Pattern Matching...

Dio - Dive Into Objects - Ruby Pattern Matching interface for Objects which have none (yet)

The general direction of my argument in this regard is that pattern expressions are exactly that: patterns. They are not Symbols, nor are they Strings, just as keyword arguments are keyword arguments and not Symbols either.

Regarding your second point? Patterns are not first-class objects, unlike a regular expression, for example. However, you can cheat this one a bit more elegantly:

captured_pattern = -> value { value in pattern: /match/ }

You use a function / proc / lambda / block to do it. That's also something I want, in addition to composable patterns (e.g., capturing a pattern and reusing it inside another one as a branch or a part of it.)

··· (click for more details)

Discussion in the ATmosphere

Loading comments...