Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,10 @@ strscan_named_captures(VALUE self)
named_captures_data data;
data.self = self;
data.captures = rb_hash_new();
if (NIL_P(p->regex)) {
return data.captures;
}

onig_foreach_name(RREGEXP_PTR(p->regex), named_captures_iter, &data);

return data.captures;
Expand Down
6 changes: 6 additions & 0 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,12 @@ def test_named_captures
assert_equal(9, scan.match?(/(?<f>foo)(?<r>bar)(?<z>baz)/))
assert_equal({"f" => "foo", "r" => "bar", "z" => "baz"}, scan.named_captures)
end

def test_named_captures_no_match
omit("not implemented on TruffleRuby") if ["truffleruby"].include?(RUBY_ENGINE)
scan = StringScanner.new("foobarbaz")
assert_equal({}, scan.named_captures)
end
end

class TestStringScanner < Test::Unit::TestCase
Expand Down