# Adding MatchData#groups to Ruby 1.9 (svn) with Oniguruma 4.4

**URL:** <https://www.ruby-forum.com/t/adding-matchdata-groups-to-ruby-1-9-svn-with-oniguruma-4-4/91870>\
**Category:** Ruby\
**Created:** [April 12, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/adding-matchdata-groups-to-ruby-1-9-svn-with-oniguruma-4-4/91870 "2007-04-12T23:17:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rretzbach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@rretzbach](https://www.ruby-forum.com/u/rretzbach)\
**Post date:** [April 12, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/adding-matchdata-groups-to-ruby-1-9-svn-with-oniguruma-4-4/91870/1 "2007-04-12T23:17:16Z")

</div>

Heyho,

I would like to propose a method for returning all named groups of a  
MatchData object as Array:

`
m = /(?\w+) (?\w+)/.match("Robert R.")
m.groups
#=> ["forename", "surname"]
lets you create an own Hash easily if the Hash-like MatchData
doesn’t suit you
Hash[*m.groups.zip(m.captures).flatten]
#=> {“forename”=>“Robert”, “surname”=>“Retzbach”}
`

This is my first time creating Ruby ext in C, so please explain to me  
any mistakes 🙂

# Here is the svn diff: ----\>8---- Index: re.c

— re.c (Revision 12173)  
+++ re.c (Arbeitskopie)  
@@ -1334,7 +1334,36 @@  
return rb\_reg\_nth\_match(n, match);  
}

+/\*

- 
  - call-seq:

- 
  - 

```
mtch.groups => array

```

- 
  - 

- 
  - Returns the array of all named groups of the Regexp.

- 
  - 

- 
  - 

```
ng = /(?<firstchar>.)(.)(\d+)(?<lastnum>

```

\d)/.match(“THX1138.”).groups

- 
  - 

```
ng[0] #=> "firstchar"

```

- 
  - 

```
ng[1] #=> "lastnum"

```

- \*/

+static int  
+i\_add\_a\_group(const UChar\* name, const UChar\* name\_end, int back\_num,  
int\* back\_refs, regex\_t\* reg, void\* arg)  
+{

- rb\_ary\_push(arg, rb\_str\_new(name, name\_end - name));
- 
- return 0;  
+}
- 

+static VALUE  
+match\_groups(VALUE match)  
+{

- VALUE ary;
- 
- ary = rb\_ary\_new();
- onig\_foreach\_name(RREGEXP(RMATCH(match)-\>regexp)-\>ptr,  
i\_add\_a\_group, ary);
- 
- return ary;  
+}
- 

/\*

- call-seq:  
if (!OBJ\_TAINTED(obj) && rb\_safe\_level() \>= 4)  
@@ -2387,6 +2416,7 @@  
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, “groups”, match\_groups, 0);  
rb\_define\_method(rb\_cMatch, “captures”, match\_captures, 0);  
rb\_define\_method(rb\_cMatch, “select”, match\_select, -1);  
rb\_define\_method(rb\_cMatch, “values\_at”, match\_values\_at, -1);  
----\>8----

I just updated the svn ruby and tried it:  
  
% ./ruby -e ‘p ng = /(?.)(.)(\d+)(?  
\d)/.match(“THX1138.”).groups’  
[“firstchar”, “lastnum”]

Now I have some questions to this extension? Is this the right place  
for such a proposal?  
Should the method groups belong rather to Regexp?  
Any other idea to improve this?

Thanks for reading.  
I should advertise this with beatiful pics of woman presenting my code  
a la ebay 🙂

---

<div class="post-metadata">

**Author:** ![rretzbach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@rretzbach](https://www.ruby-forum.com/u/rretzbach)\
**Post date:** [April 13, 2007, 3:34am UTC](https://www.ruby-forum.com/t/adding-matchdata-groups-to-ruby-1-9-svn-with-oniguruma-4-4/91870/2 "2007-04-13T03:34:32Z")

</div>

Hi,

At Fri, 13 Apr 2007 06:15:10 +0900,  
rretzbach wrote in [ruby-talk:247724]:

> Now I have some questions to this extension? Is this the right place  
> for such a proposal?

ruby-core ML would be a better place.

> Should the method groups belong rather to Regexp?

+1

> Any other idea to improve this?

It feels nice if MatchData has a method returns the Hash-like  
MatchData. Although I don’t have an idea for the name.

---

<div class="post-metadata">

**Author:** ![rretzbach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@rretzbach](https://www.ruby-forum.com/u/rretzbach)\
**Post date:** [April 13, 2007, 10:55am UTC](https://www.ruby-forum.com/t/adding-matchdata-groups-to-ruby-1-9-svn-with-oniguruma-4-4/91870/3 "2007-04-13T10:55:40Z")

</div>

On 13 Apr., 03:33, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Should the method groups belong rather to Regexp?
> 
> +1
> 
> > Any other idea to improve this?
> 
> It feels nice if MatchData has a method returns the Hash-like  
> MatchData. Although I don’t have an idea for the name.
> 
> –  
> Nobu Nakada

MatchData#to\_h maybe 😉

If this thread drows here I will post it in ruby core again.  
Thanks for your comment.
