mruby-regexp: return Arrays from Regexp#named_captures - #7026
Merged
Conversation
`Regexp#named_captures` handed out the internal name to group-number
table, so its values were Integers and every caller shared one Hash.
```ruby
/(?<a>x)/.named_captures # CRuby: {"a" => [1]}, mruby: {"a" => 1}
re = /(?<a>x)/
re.named_captures["a"] = 99
re.named_captures # CRuby: {"a" => [1]}, mruby: {"a" => 99}
```
CRuby uses an Array because one name can map to several group numbers,
as in `/(?<x>a)|(?<x>b)/`. mruby-regexp does not resolve duplicate names
correctly yet, which is a separate defect in the compiler and in name
resolution; this change only aligns the return type so that portable
code can be written today.
Derive a fresh Hash from the `@named_captures` table on every call and
wrap each group number in an Array. The instance variable keeps holding
the internal name to group-number table that `regexp_init()` fills.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthrough
ChangesNamed capture results
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This was referenced Aug 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Regexp#named_capturesreturns Integer values where CRuby returns Arrays.It also hands out the Hash stored in the
@named_capturesinstance variable, so acaller that writes into the returned Hash corrupts the Regexp for every later caller:
CRuby uses an Array because one name can map to several group numbers, as in
/(?<x>a)|(?<x>b)/. mruby-regexp accepts such a pattern but does not handle itcorrectly yet: the later group silently overwrites the earlier one in the Hash, and a
name lookup resolves to the first entry in the C table regardless of which alternative
matched. Duplicate names therefore still resolve wrongly with this change applied:
That is a separate defect in the compiler and in name resolution, left out of this
change. The return type is aligned so that portable code can be written today.
Changes
Regexp#named_capturesderives a fresh Hash from the@named_capturestable onevery call and wraps each group number in an Array.
regexp_init()keeps filling theinstance variable with the internal name to group-number table, and the public method
becomes a derivation of it, which is also where the shared-Hash aliasing goes away.
MatchData#named_capturesis unaffected. It rebuilds its result from the compiled Ctable on every call and already answers name to captured string.
Regexp#named_capturesis added to the Ruby API block inmrbgems/mruby-regexp/README.md.Tests
A new
assert("Regexp#named_captures")inmrbgems/mruby-regexp/test/regexp.rbcoversthe Array value type, the empty Hash for a pattern with no named group, and that
mutating the returned Hash does not affect a later call.
rake testpasses.Summary by CodeRabbit
New Features
Regexp#named_capturesto return each capture name with its corresponding group index.Bug Fixes
Documentation
named_capturesreturn format.