Skip to content

MatchData#named_captures: add optional symbolize_names keyword #6952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

palkan
Copy link
Contributor

@palkan palkan commented Dec 17, 2022

Feature #19591

Follow-up for #6216.

Example:

m = /(?<a>.)(?<a>.)/.match("01")  # => #<MatchData "01" a:"0" a:"1">
m.named_captures #=> {"a" => "1"}
m.named_captures(symbolize_names: true) #=> {:a => "1"}

The actual functionality has been implemented as a part of MatchData#deconstruct_keys, where we needed symbolic keys. This PR proposes to expose it to the #named_captures method via the optional symbolize_names: true | false keyword argument.

@ioquatix
Copy link
Member

For consistency, please consider using symbolize_keys

ruby/psych#341

@palkan palkan changed the title MatchData#named_captures: add optional symbolize_names keyword MatchData#named_captures: add optional symbolize_keys keyword Dec 18, 2022
@palkan palkan force-pushed the feat/match-data-named-captures-symbolize-names branch 2 times, most recently from 5104535 to d837838 Compare December 18, 2022 00:20
@palkan
Copy link
Contributor Author

palkan commented Dec 18, 2022

@nobu @ioquatix Thanks for the reviews/suggestions. Updated accordingly.

@ioquatix
Copy link
Member

I suggest you add tests to the ruby specs too.

@palkan palkan force-pushed the feat/match-data-named-captures-symbolize-names branch 2 times, most recently from 2280236 to 29d4074 Compare December 19, 2022 13:22
@ioquatix
Copy link
Member

Did Matz already accept this feature?

@palkan
Copy link
Contributor Author

palkan commented Dec 19, 2022

Did Matz already accept this feature?

Don't think so. We only discussed it with Kazuki in the context of MatchData#deconstruct_keys here.

@ioquatix
Copy link
Member

Okay, let me see if I can pull Matz into the discussion.

@ioquatix
Copy link
Member

Okay, this needs a new issue on bugs.ruby-lang.org. If you can do it, please outline the new feature and link to this PR. It can be brief as this feature is not complex.

@palkan
Copy link
Contributor Author

palkan commented Apr 11, 2023

Opened a ticket https://bugs.ruby-lang.org/issues/19591 (finally).

@palkan palkan force-pushed the feat/match-data-named-captures-symbolize-names branch 4 times, most recently from 6c10db9 to 8e2587d Compare April 15, 2023 14:55
@palkan palkan changed the title MatchData#named_captures: add optional symbolize_keys keyword MatchData#named_captures: add optional symbolize_names keyword Apr 15, 2023
@palkan
Copy link
Contributor Author

palkan commented Apr 15, 2023

Reverted back to symbolize_names as per Matz request.

@ioquatix
Copy link
Member

ioquatix commented Apr 18, 2023

@palkan do you agree with calling it symbolize_names?

I spent a few days thinking about whether I want to spend energy on this topic.

Honestly, I would like to clarify the naming convention because I don't think JSON symbolize_names is a good standard to follow. So, let's try to standardise the existing convention, e.g. https://bugs.ruby-lang.org/issues/19607 and I will ask Matz about It at the next developer meeting.

@palkan
Copy link
Contributor Author

palkan commented Apr 18, 2023

Yeah, I’m fine with symbolize_names. That was my original suggestion and I also referred to JSON API; although the naming (“names”) may be not ideal, developers are familiar with it. So, the principle of least surprise works here.

(Also, now I think that symbolize_names is more natural for #named_captures since we already use “named” here, we already refer to these entities as names).

@ioquatix
Copy link
Member

Yeah, named_captures and symbolize_names makes sense to me too.

Regarding JSON, I went and looked at the RFC. They actually refer to it as a name:

An object is an unordered collection of zero or more name/value
pairs, where a name is a string and a value is a string, number,
boolean, null, object, or array.

So in this regard, maybe JSON.parse(..., symbolize_names:) makes sense but for different reasons.

@ioquatix ioquatix force-pushed the feat/match-data-named-captures-symbolize-names branch from 8e2587d to 516c290 Compare April 18, 2023 21:55
@ioquatix ioquatix merged commit b09f5c7 into ruby:master Apr 18, 2023
@palkan palkan deleted the feat/match-data-named-captures-symbolize-names branch April 18, 2023 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants