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

R

rretzbach

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

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 :)
 
N

Nobuyoshi Nakada

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.
 
R

rretzbach

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.

MatchData#to_h maybe ;)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top