[#107867] Fwd: [ruby-cvs:91197] 8f59482f5d (master): add some tests for Unicode Version 14.0.0 — Martin J. Dürst <duerst@...>
To everybody taking care of continuous integration:
3 messages
2022/03/13
[#108090] [Ruby master Bug#18666] No rule to make target 'yaml/yaml.h', needed by 'api.o' — duerst <noreply@...>
Issue #18666 has been reported by duerst (Martin D端rst).
7 messages
2022/03/28
[#108117] [Ruby master Feature#18668] Merge `io-nonblock` gems into core — "Eregon (Benoit Daloze)" <noreply@...>
Issue #18668 has been reported by Eregon (Benoit Daloze).
22 messages
2022/03/30
[ruby-core:107950] [Ruby master Feature#18583] Pattern-matching: API for custom unpacking strategies?
From:
"palkan (Vladimir Dementyev)" <noreply@...>
Date:
2022-03-17 13:10:52 UTC
List:
ruby-core #107950
Issue #18583 has been updated by palkan (Vladimir Dementyev).
> This, though, raises a question of several match groups, at which point one starts to want more:
> ```ruby
> case string
> in /{{(.+?): (.+?)}}/ => [key, value]
> # use key and value
> in /{{=(?<named>.+?)}}/ => {named:}
> # use named
> ```
> ...so... IDK.
This one could be achieve via guards:
```ruby
case val
in /(foo|bar)/ if $~ in [val]
puts val
in /(?<named>\d+)/ if $~ in {named: }
puts named
end
```
That would require adding MatchData#{deconstruct,deconstruct_keys}, though:
```ruby
refine MatchData do
alias deconstruct captures
def deconstruct_keys(*)
named_captures.transform_keys(&:to_sym)
end
end
```
Regarding the original proposal (the unpacking API), I think, it could bring more confusion than value. Adding one more _implicit_ layer (in addition to `#deconstruct` and `#deconstruct_keys`, which could also be overridden) would make pattern matching even more _magical_ in a bad sense.
----------------------------------------
Feature #18583: Pattern-matching: API for custom unpacking strategies?
https://bugs.ruby-lang.org/issues/18583#change-96900
* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
----------------------------------------
I started to think about it when discussing https://github.com/ruby/strscan/pull/30.
The thing is, usage of StringScanner for many complicated parsers invokes some kind of branching.
In pseudocode, the "ideal API" would allow to write something like this:
```ruby
case <what next matches>
in /regexp1/ => value_that_matched
# use value_that_matched
in /regexp2/ => value_that_matched
# use value_that_matched
# ...
```
This seems "intuitively" that there *should* be some way of implementing it, but we fall short. We can do some StringScanner-specific matcher object which defines its own `#===` and use it with pinning:
```ruby
case scanner
in ^(Matcher.new(/regexp1/)) => value_that_matched
# ...
```
But there is no API to tell how the match result will be unpacked, just the whole `StringScanner` will be put into `value_that_matched`.
So, I thought that maybe it would be possible to define some kind of API for pattern-like objects, the method with signature like `try_match_pattern(value)`, which by default is implemented like `return value if self === value`, but can be redefined to return something different, like part of the object, or object transformed somehow.
This will open some interesting (if maybe uncanny) possibilities: not just slicing out the necessary part, but something like
```ruby
value => ^(type_caster(Integer)) => int_value
```
So... Just a discussion topic!
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>