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

Heyho,

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

<code>
m = /(?<forename>\w+) (?<surname>\w+)/.match("Robert Retzbach")
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"}
</code>

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

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:
<shell>
% ./ruby -e 'p ng = /(?<firstchar>.)(.)(\d+)(?<lastnum>
\d)/.match("THX1138.").groups'
["firstchar", "lastnum"]
</shell>

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 :slight_smile:

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.

路路路

--
Nobu Nakada

MatchData#to_h maybe :wink:

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

路路路

On 13 Apr., 03:33, Nobuyoshi Nakada <n...@ruby-lang.org> wrote:

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.

--
Nobu Nakada