MatchData#{key?,has_key?}

MatchData e$B$G!"$"$kItJ,J8;zNs$,B8:_$9$k$+$I$&$+$rD4$Y$ke(B
MatchData#has_key?(n), MatchData#key?(n) e$B$rDI2C$9$k$N$O$I$&e(B
e$B$G$7$g$&$+!#e(B

e$B$3$l$,$J$/$F$be(B MatchData#[n]
e$B$GD4$Y$i$l$^$9$,!"$3$l$OJ8;zNs$re(B
e$B@8@.$9$k$N$G8zN(>e4r$7$/$"$j$^$;$s!#e(B

1.8 e$B$G$Oe(B MatchData#begin(n) e$B$,;H$($?$N$G$9$,!"$3$l$Oe(B 1.9
e$B$Ge(B
e$BJ8;zNs$ND9$5$KHfNc$7$?;~4V$,$+$+$k$h$&$K$J$C$F$$$k$N$G;H$($^e(B
e$B$;$s!#e(B

Index: re.c

— re.c (revision 15432)
+++ re.c (working copy)
@@ -1396,6 +1396,40 @@ match_aref(int argc, VALUE *argv, VALUE
}

static VALUE
+match_has_key(VALUE match, VALUE idx)
+{

  • int nth;
  • VALUE name, regexp;
  • if (FIXNUM_P(idx)) {
  •    nth = FIX2INT(idx);
    
  • }
  • else {
  •    if (TYPE(idx) == T_SYMBOL)
    
  •        name = rb_id2str(SYM2ID(idx));
    
  •    else
    
  •        name = StringValue(idx);
    
  •    regexp = RMATCH(match)->regexp;
    
  •    nth = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
    
  •              (const unsigned char*)RSTRING_PTR(name),
    
  •              (const unsigned char*)RSTRING_PTR(name) + 
    

RSTRING_LEN(name),

  •              RMATCH(match)->regs);
    
  •    if (nth < 1)
    
  •        return Qnil;
    
  • }
  • if (nth >= RMATCH(match)->regs->num_regs) {
  •    return Qnil;
    
  • }
  • if (nth < 0) {
  •    nth += RMATCH(match)->regs->num_regs;
    
  •    if (nth <= 0) return Qnil;
    
  • }
  • if (RMATCH(match)->BEG(nth) == -1)
  •    return Qfalse;
    
  • return Qtrue;
    +}

+static VALUE
match_entry(VALUE match, long n)
{
return rb_reg_nth_match(n, match);
@@ -3104,6 +3138,8 @@ Init_Regexp(void)
rb_define_method(rb_cMatch, “end”, match_end, 1);
rb_define_method(rb_cMatch, “to_a”, match_to_a, 0);
rb_define_method(rb_cMatch, “[]”, match_aref, -1);

  • rb_define_method(rb_cMatch, “has_key?”, match_has_key, 1);
  • rb_define_method(rb_cMatch, “key?”, match_has_key, 1);
    rb_define_method(rb_cMatch, “captures”, match_captures, 0);
    rb_define_method(rb_cMatch, “values_at”, match_values_at, -1);
    rb_define_method(rb_cMatch, “select”, match_values_at, -1);