[#108461] [Ruby master Bug#18762] Add an Array#undigits that compliments Integer#digits — "shan (Shannon Skipper)" <noreply@...>

Issue #18762 has been reported by shan (Shannon Skipper).

8 messages 2022/05/02

[#108499] [Ruby master Bug#18767] IO.foreach hangs up when passes limit=0 — "andrykonchin (Andrew Konchin)" <noreply@...>

Issue #18767 has been reported by andrykonchin (Andrew Konchin).

9 messages 2022/05/10

[#108500] [Ruby master Bug#18768] Inconsistent behavior of IO, StringIO and String each_line methods when return paragraph and chomp: true passed — "andrykonchin (Andrew Konchin)" <noreply@...>

Issue #18768 has been reported by andrykonchin (Andrew Konchin).

7 messages 2022/05/10

[#108511] [Ruby master Feature#18773] deconstruct to receive a range — "kddeisz (Kevin Newton)" <noreply@...>

Issue #18773 has been reported by kddeisz (Kevin Newton).

12 messages 2022/05/11

[#108514] [Ruby master Feature#18774] Add Queue#pop(timeout:) — "Eregon (Benoit Daloze)" <noreply@...>

Issue #18774 has been reported by Eregon (Benoit Daloze).

17 messages 2022/05/11

[#108522] [Ruby master Feature#18776] Object Shapes — "jemmai (Jemma Issroff)" <noreply@...>

Issue #18776 has been reported by jemmai (Jemma Issroff).

25 messages 2022/05/11

[#108543] [Ruby master Bug#18779] `GC.compact` and other compaction related methods should be defined as rb_f_notimplement on non supported platforms. — "byroot (Jean Boussier)" <noreply@...>

Issue #18779 has been reported by byroot (Jean Boussier).

10 messages 2022/05/13

[#108546] [Ruby master Bug#18780] Incorrect binding receiver for C API rb_eval_string() — "daveola (David Stellar)" <noreply@...>

Issue #18780 has been reported by daveola (David Stellar).

21 messages 2022/05/13

[#108549] [Ruby master Bug#18781] MJIT tests failing with Ubuntu focal with gcc-11 and some flags — "jaruga (Jun Aruga)" <noreply@...>

Issue #18781 has been reported by jaruga (Jun Aruga).

8 messages 2022/05/14

[#108552] [Ruby master Bug#18782] Race conditions in autoload when loading the same feature with multiple threads. — "ioquatix (Samuel Williams)" <noreply@...>

Issue #18782 has been reported by ioquatix (Samuel Williams).

11 messages 2022/05/14

[#108565] [Ruby master Bug#18784] `FileUtils.rm_f` and `FileUtils.rm_rf` should not mask exceptions — deivid <noreply@...>

Issue #18784 has been reported by deivid (David Rodr鱈guez).

33 messages 2022/05/16

[#108590] [Ruby master Feature#18788] Support passing Regexp options as String to Regexp.new — janosch-x <noreply@...>

Issue #18788 has been reported by janosch-x (Janosch M端ller).

10 messages 2022/05/17

[#108659] [Ruby master Bug#18798] `UnboundMethod#==` with inherited classes — "ko1 (Koichi Sasada)" <noreply@...>

Issue #18798 has been reported by ko1 (Koichi Sasada).

16 messages 2022/05/24

[#108708] [Ruby master Bug#18808] Cannot compile ruby 3.1.2 on powerpc64le-linux without disabling the jit features — "npn (John Davis)" <noreply@...>

Issue #18808 has been reported by npn (John Davis).

17 messages 2022/05/26

[#108724] [Ruby master Feature#18809] Add Numeric#ceildiv — "kyanagi (Kouhei Yanagita)" <noreply@...>

Issue #18809 has been reported by kyanagi (Kouhei Yanagita).

9 messages 2022/05/27

[#108728] [Ruby master Bug#18810] Make `Kernel#p` interruptable. — "ioquatix (Samuel Williams)" <noreply@...>

Issue #18810 has been reported by ioquatix (Samuel Williams).

13 messages 2022/05/28

[ruby-core:108480] [Ruby master Feature#18384] Pattern Match Object

From: "kddeisz (Kevin Newton)" <noreply@...>
Date: 2022-05-07 18:01:14 UTC
List: ruby-core #108480
Issue #18384 has been updated by kddeisz (Kevin Newton).


There are a lot of places I would use this with pattern matching. Being able to pass around an object that for sure matches something would be really useful for Syntax Tree.

I would like to see something that is a little more explicit than passing around hashes or increased syntax though. I think we can get away with introducing a method like deconstruct_keys that does the opposite. Right now when you pass an object into a case predicate, it calls deconstruct_keys or deconstruct when you match against hash or array patterns respectively. I think we could do the opposite with something like construct. For example:

```ruby
class MyMatcher
  def construct(value)
    value in { foo: 1, bar: 2 }
  end
end

case my_object
in MyMatcher.new
  # do something
end
```

In this case you would just need the convention that `construct` is called with the object that is being checked. Then you could pass around your matching objects however you wanted, like for instance passing them back to the consumer of a library to match against their own tree structures.


----------------------------------------
Feature #18384: Pattern Match Object
https://bugs.ruby-lang.org/issues/18384#change-97524

* Author: baweaver (Brandon Weaver)
* Status: Open
* Priority: Normal
----------------------------------------
Related to discussion in #18369 it might be nice to have a literal syntax for constructing a single pattern match case outside of a one-liner.

Years ago in Qo I had done this via `===` to enable syntax like this:

```ruby
list_of_people.select(&Qo[first_name: /^F/, last_name: /r$/, age: 20..40])
```

This is valid Ruby, but the pattern match syntax itself cannot be used outside of a literal match, making this impossible without syntax changes.

### Proposal

My proposal would be a case which would be very useful in predicate methods (`any?`, `all?`, etc) and triple-equals responding methods (`select`, `reject`, `grep`, etc):

```ruby
list_of_people.select(&pattern(
  first_name: /^F/,
  last_name: /r$/,
  age: 20..40 
))
```

...in which `pattern` would be substituted with a more appropriate name which I cannot think of at the moment.

### Portability

Now the reason I think this could be very interesting is the portability of patterns. Consider the potential of making a `PatternMatch` object much like a Regular Expression:

```ruby
TARGET_PERSON = PatternMatch.new(first_name: 'something')
list_of_people.select(&TARGET_PERSON)
```

As they can serve similar purposes of giving an expressive language to query against known structures I can see this making sense. The challenge is that the initialization of such an object would need to be special to accommodate the pattern matching syntax, adding more complicated parsing rules.

This behavior might be consistent with `Proc`, `RegExp`, and `Range`-like behavior.

### ActiveRecord-like

This gets very close to the classic ActiveRecord `where` pattern:

```ruby
People.where(age: 20..30)
```

### Potential Issues

Now this is not without potential issue, as must be highlighted. The first, as just mentioned, is the ActiveRecord syntax and potentially overloading that and keyword arguments:

```ruby
People.where(age: 20..30)
```

Without a clear signifier this could make parsing much more difficult.

### Current Viable Workarounds

It also must be mentioned that this is currently possible:

```ruby
list_of_people.select { _1 in { first_name: 'something' } }
```

...though the requirement of explicit braces feels a tinge verbose, I understand why they're present.

I think this is an acceptable compromise at the moment, but feel we're very close to an interesting syntactic breakthrough.



-- 
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>

In This Thread

Prev Next