From 0db6c4768b5e94c67ede9ba97b27faa36f7fb1b8 Mon Sep 17 00:00:00 2001 From: Randy Stauner Date: Fri, 13 Dec 2024 10:35:26 -0700 Subject: [PATCH] Support `symbolize_names` argument to `MatchData#named_captures` --- CHANGELOG.md | 1 + spec/tags/core/matchdata/named_captures_tags.txt | 2 -- src/main/ruby/truffleruby/core/match_data.rb | 4 +++- 3 files changed, 4 insertions(+), 3 deletions(-) delete mode 100644 spec/tags/core/matchdata/named_captures_tags.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index f3e6eb787870..c2193bd20c21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Compatibility: * Add `Dir.for_fd` (#3681, @andrykonchin). * Add `Dir.fchdir` (#3681, @andrykonchin). * Add `Dir#chdir` (#3681, @andrykonchin). +* Support `symbolize_names` argument to `MatchData#named_captures` (#3681, @rwstauner). Performance: diff --git a/spec/tags/core/matchdata/named_captures_tags.txt b/spec/tags/core/matchdata/named_captures_tags.txt deleted file mode 100644 index 3a59edb70403..000000000000 --- a/spec/tags/core/matchdata/named_captures_tags.txt +++ /dev/null @@ -1,2 +0,0 @@ -fails:MatchData#named_captures returns a Hash with Symbol keys when symbolize_names is provided a true value -fails:MatchData#named_captures returns a Hash with String keys when symbolize_names is provided a false value diff --git a/src/main/ruby/truffleruby/core/match_data.rb b/src/main/ruby/truffleruby/core/match_data.rb index d36c184c019c..6637e84d3732 100644 --- a/src/main/ruby/truffleruby/core/match_data.rb +++ b/src/main/ruby/truffleruby/core/match_data.rb @@ -91,7 +91,9 @@ def names regexp.names end - def named_captures + def named_captures(symbolize_names: false) + names = Primitive.regexp_names(self.regexp).map(&:first) + names.map!(&:to_s) unless symbolize_names names.collect { |name| [name, self[name]] }.to_h end