Project

General

Profile

Actions

Feature #19634

open

Pattern matching dynamic key

Added by baweaver (Brandon Weaver) over 1 year ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:113435]

Description

I found myself in a situation (stable marriage problem) where I would like to match against a dynamic key, like so:

mentor_proposals = { mentor_name: ['mentee_1', 'mentee_2'] }
mentor_name = :mentor_name
mentee_name = 'mentee_1'

mentor_proposals in ^key: [*, ^mentee_name, *] # SyntaxError

Currently this is not supported syntax, but there are some use cases in which I might see myself wanting to use it including this one. As deconstruct_keys currently accepts an Array of keys this would not break compatibility but would introduce syntactic complexity in capturing keys on hash-like matches.

I believe the tradeoff is worthwhile, but would like to hear others opinions on the matter.

Granted this case has some oddities of Symbol and String interchangeability as an implementation detail, and I will not be arguing for key irreverence in this issue as that's a much more involved topic.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0