Skip to content
Merged
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
7 changes: 5 additions & 2 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ public IRubyObject fixed_anchor_p(ThreadContext context) {
}

@JRubyMethod(name = "named_captures")
public IRubyObject named_captured(ThreadContext context) {
public IRubyObject named_captures(ThreadContext context) {
Ruby runtime = context.runtime;
IRubyObject nil = context.nil;

Expand All @@ -872,7 +872,10 @@ public IRubyObject named_captured(ThreadContext context) {
IRubyObject value = nil;

for (int i : nameEntry.getBackRefs()) {
value = extractRegion(context, i);
IRubyObject v = extractRegion(context, i);
if (v != nil) {
value = v;
}
}

int nameP = nameEntry.nameP;
Expand Down
5 changes: 4 additions & 1 deletion ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,10 @@ named_captures_iter(const OnigUChar *name,
VALUE value = RUBY_Qnil;
int i;
for (i = 0; i < back_num; i++) {
value = strscan_aref(data->self, INT2NUM(back_refs[i]));
VALUE v = strscan_aref(data->self, INT2NUM(back_refs[i]));
if (!RB_NIL_P(v)) {
value = v;
}
}
rb_hash_aset(data->captures, key, value);
return 0;
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 @@ -967,6 +967,12 @@ def test_named_captures
assert_equal({}, scan.named_captures)
end

def test_named_captures_same_name_union
scan = StringScanner.new("123")
assert_equal(1, scan.match?(/(?<number>0)|(?<number>1)|(?<number>2)/))
assert_equal({"number" => "1"}, scan.named_captures)
end

def test_scan_integer
s = create_string_scanner('abc')
assert_equal(3, s.match?(/(?<a>abc)/)) # set named_captures
Expand Down