ruby icon indicating copy to clipboard operation
ruby copied to clipboard

Add MatchData#deconstruct/deconstruct_keys

Open palkan opened this issue 3 years ago • 3 comments

Feature #18821

The #deconstruct_keys behaviour is inferred from the Struct#deconstruct_keys, in particular:

  1. Both Symbols and Strings could be passed as the required keys, the type is preserved.
  2. If the number of the passed keys is greater than the number of the named groups, an empty Hash is returned.
  3. Collecting the keys stops as soon as an unknown key is encountered; the returned Hash contains all the keys collected so far.

IMO, 1) and 2) are questionable. But since we already do that for Structs, it makes sense to preserve the current behaviors and not introduce a new one. Alternatively, we can re-visit the Struct API (which I proposed once, but it was rejected).

/cc @baweaver

palkan avatar Aug 06 '22 00:08 palkan

@palkan Sorry for late review. Could you update NEWS.md if possible? Then, I'll merge this.

  1. Both Symbols and Strings could be passed as the required keys, the type is preserved.
  2. If the number of the passed keys is greater than the number of the named groups, an empty Hash is returned.

These are not required. In case of Struct, I implemented them this way for performance.

k-tsj avatar Sep 08 '22 08:09 k-tsj

@k-tsj Thanks for the review! Updated.

palkan avatar Sep 10 '22 01:09 palkan

@palkan Thanks for updating NEWS! I added some review comments, could you check it?

k-tsj avatar Sep 10 '22 04:09 k-tsj