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

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

% ./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 :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.

On 13 Apr., 03:33, Nobuyoshi N. [email protected] 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 :wink:

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