From cd416419fa322ddcadd3e26dfe1031b557849c92 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, 3 insertions(+), 4 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..bb3e70839e6b 100644 --- a/src/main/ruby/truffleruby/core/match_data.rb +++ b/src/main/ruby/truffleruby/core/match_data.rb @@ -91,8 +91,8 @@ def names regexp.names end - def named_captures - names.collect { |name| [name, self[name]] }.to_h + def named_captures(symbolize_names: false) + names.collect { |name| [symbolize_names ? name.to_sym : name, self[name]] }.to_h end def begin(index)