# MatchData#{key?,has\_key?}

**URL:** <https://www.ruby-forum.com/t/matchdata-key-has-key/129118>\
**Category:** Development\
**Created:** [February 14, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/matchdata-key-has-key/129118 "2008-02-14T16:04:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [February 14, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/matchdata-key-has-key/129118/1 "2008-02-14T16:04:52Z")

</div>

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);
