Issue #14709 has been updated by baweaver (Brandon Weaver).


It should also be mentioned that the way I achieved the pattern matching mentioned above was by using a status container in the style of Elixir (`{:ok, result}` except `[true, result]`):

https://github.com/baweaver/qo/blob/83577f80a47015e60d833da62a1220a08c00482d/lib/qo/matchers/pattern_match.rb#L77-L91

```ruby
# Immediately invokes a PatternMatch
#
# @param target [Any]
#   Target to run against and pipe to the associated block if it
#   "matches" any of the GuardBlocks
#
# @return [Any | nil] Result of the piped block, or nil on a miss
def call(target)
  @matchers.each { |guard_block_matcher|
    did_match, match_result = guard_block_matcher.call(target)
    return match_result if did_match
  }

  nil
end
```

By inheriting from a matcher and overriding its `to_proc` method, one can use `super` to get the boolean result of the matcher. If that result is true, we can invoke the block function passed to the matcher to simulate Right Hand Assignment:

https://github.com/baweaver/qo/blob/83577f80a47015e60d833da62a1220a08c00482d/lib/qo/matchers/guard_block_matcher.rb#L38-L48

```ruby
# Overrides the base matcher's #to_proc to wrap the value in a status
# and potentially call through to the associated block if a base
# matcher would have passed
#
# @return [Proc[Any] - (Bool, Any)]
#   (status, result) tuple
def to_proc
  Proc.new { |target|
    super[target] ? [true, @fn.call(target)] : NON_MATCH
  }
end
```

This is done to prevent false negatives from legitimate falsey values that might be returned as a result of a match, and will be a similar concern in implementation.

----------------------------------------
Feature #14709: Proper pattern matching
https://bugs.ruby-lang.org/issues/14709#change-71633

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
On RubyKaigi 2017, there was a [presentation](http://rubykaigi.org/2017/presentations/yotii23.html) of Yuki Torii about possible implementation of pattern matching.

The syntax proposed in presentation was:

```ruby
res = [:ng, 500]
case res
when %p([:ng, status])
  p status
end
```

The proposed syntax seem to feel pretty consistent, and the implementation (forked Ruby interpreter) was working at this moment.

As @ko1 was one of the contributors to the experiment, I don't suppose Ruby core team is not aware of the proposal, so I'd like to know what the status of it? Are there some plans for full-strength pattern matching in Ruby 3 (with proposed, or any other, syntax)?

PS: There are many existing gems with some kind "almost real" pattern matching (including recently emerged [Qo](https://github.com/baweaver/qo)), yet I believe that the _only_ useful pattern matching can be provided language core. Otherwise, two main goals can't be achieved:
* reasonable performance (as the pattern-matching is useful mostly in complicated algorithms, which tend to repeat matches thousands of times);
* unpacking of parts of the patterns into local variables.



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