@@ -810,27 +810,25 @@ public RubyFixnum hash() {
810
810
811
811
@ JRubyMethod
812
812
public RubyHash named_captures (ThreadContext context ) {
813
+ check ();
813
814
Ruby runtime = context .runtime ;
814
-
815
815
RubyHash hash = RubyHash .newHash (runtime );
816
-
817
- if (regexp .getPattern ().numberOfNames () > 0 ) {
818
- Iterator <NameEntry > nameEntryIterator = regexp .getPattern ().namedBackrefIterator ();
819
- while (nameEntryIterator .hasNext ()) {
820
- NameEntry entry = nameEntryIterator .next ();
821
- RubyString key = RubyString .newStringShared (runtime , new ByteList (entry .name , entry .nameP , entry .nameEnd - entry .nameP , regexp .getEncoding (), false ));
822
- boolean found = false ;
823
-
824
- for (int i : entry .getBackRefs ()) {
825
- IRubyObject value = RubyRegexp .nth_match (i , this );
826
- if (value .isTrue ()) {
827
- hash .op_asetForString (runtime , key , value );
828
- found = true ;
829
- }
816
+ if (regexp == context .nil ) return hash ;
817
+
818
+ for (Iterator <NameEntry > i = regexp .pattern .namedBackrefIterator (); i .hasNext ();) {
819
+ NameEntry entry = i .next ();
820
+ RubyString key = RubyString .newStringShared (runtime , new ByteList (entry .name , entry .nameP , entry .nameEnd - entry .nameP , regexp .getEncoding (), false ));
821
+ boolean found = false ;
822
+
823
+ for (int b : entry .getBackRefs ()) {
824
+ IRubyObject value = RubyRegexp .nth_match (b , this );
825
+ if (value .isTrue ()) {
826
+ hash .op_asetForString (runtime , key , value );
827
+ found = true ;
830
828
}
831
-
832
- if (!found ) hash .op_asetForString (runtime , key , context .nil );
833
829
}
830
+
831
+ if (!found ) hash .op_asetForString (runtime , key , context .nil );
834
832
}
835
833
836
834
return hash ;
0 commit comments