Skip to content

Commit 82b3f94

Browse files
committed
Implement MatchData#named_captures.
PullRequest: truffleruby/488
2 parents cfedcc9 + 94d9b67 commit 82b3f94

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
New features:
44

5-
* Implemented `Dir.each_child`.
65
* macOS clocks `CLOCK_MONOTONIC_RAW`, `_MONOTONIC_RAW_APPROX`, `_UPTIME_RAW`,
76
`_UPTIME_RAW_APPROX`, and `_PROCESS_CPUTIME_ID` have been implemented (#1480).
87

98
Bug fixes:
109

11-
* Adding missing support for the `close_others` option to `exec` and `spawn`.
1210
* FFI::Pointer now does the correct range checks for signed and unsigned values.
1311
* Allow signal `0` to be used with `Process.kill` (#1474).
1412
* `IO#dup` now properly sets the new `IO` instance to be close-on-exec.
1513
* `IO#reopen` now properly resets the receiver to be close-on-exec.
1614
* `StringIO#set_encoding` no longer raises an exception if the underlying
1715
`String` is frozen (#1473).
1816

17+
Compatibility:
18+
19+
* Implemented `Dir.each_child`.
20+
* Adding missing support for the `close_others` option to `exec` and `spawn`.
21+
* Implemented the missing `MatchData#named_captures` method (#1512).
22+
1923
Changes:
2024

2125
* `Process::CLOCK_` constants have been given the same value as in standard

spec/ruby/core/matchdata/named_captures_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
it 'prefers later captures' do
1010
/\A(?<a>.)(?<b>.)(?<b>.)(?<a>.)\z/.match('0123').named_captures.should == { 'a' => '3', 'b' => '2' }
1111
end
12+
13+
it 'returns the latest matched capture, even if a later one that does not match exists' do
14+
/\A(?<a>.)(?<b>.)(?<b>.)(?<a>.)?\z/.match('012').named_captures.should == { 'a' => '0', 'b' => '2' }
15+
end
1216
end
1317
end

spec/tags/core/matchdata/named_captures_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/ruby/core/regexp.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ def names
365365
regexp.names
366366
end
367367

368+
def named_captures
369+
names.collect { |name| [name, self[name]] }.to_h
370+
end
371+
368372
def pre_match_from(idx)
369373
source = Truffle.invoke_primitive(:match_data_get_source, self)
370374
return source.byteslice(0, 0) if self.byte_begin(0) == 0

0 commit comments

Comments
 (0)