From 86e23529ad161643e011aa09e14c83bbdd767c63 Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Mon, 18 Apr 2022 14:34:40 -0500 Subject: Enhanced RDoc for MatchData (#5820) Treats: #pre_match #post_match #to_a #captures --- re.c | 74 ++++++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 33 deletions(-) (limited to 're.c') diff --git a/re.c b/re.c index 4b9f2a3849..36bc026d34 100644 --- a/re.c +++ b/re.c @@ -1880,13 +1880,19 @@ rb_reg_last_match(VALUE match) /* * call-seq: - * mtch.pre_match -> str + * pre_match -> string * - * Returns the portion of the original string before the current match. - * Equivalent to the special variable $`. + * Returns the substring of the target string from its beginning + * up to the first match in +self+ (that is, self[0]); + * equivalent to regexp global variable $`: + * + * m = /(.)(.)(\d+)(\d)/.match("THX1138.") + * # => # + * m[0] # => "HX1138" + * m.pre_match # => "T" + * + * Related: MatchData#post_match. * - * m = /(.)(.)(\d+)(\d)/.match("THX1138.") - * m.pre_match #=> "T" */ VALUE @@ -1906,13 +1912,20 @@ rb_reg_match_pre(VALUE match) /* * call-seq: - * mtch.post_match -> str + * post_match -> str * - * Returns the portion of the original string after the current match. - * Equivalent to the special variable $'. + * Returns the substring of the target string from + * the end of the first match in +self+ (that is, self[0]) + * to the end of the string; + * equivalent to regexp global variable $': + * + * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") + * # => # + * m[0] # => "HX1138" + * m.post_match # => ": The Movie"\ + * + * Related: MatchData.pre_match. * - * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") - * m.post_match #=> ": The Movie" */ VALUE @@ -2001,24 +2014,16 @@ match_array(VALUE match, int start) /* * call-seq: - * mtch.to_a -> anArray + * to_a -> array * - * Returns the array of matches. + * Returns the array of matches: + * + * m = /(.)(.)(\d+)(\d)/.match("THX1138.") + * # => # + * m.to_a #=> ["HX1138", "H", "X", "113", "8"] + * + * Related: MatchData#captures. * - * m = /(.)(.)(\d+)(\d)/.match("THX1138.") - * m.to_a #=> ["HX1138", "H", "X", "113", "8"] - * - * Because to_a is called when expanding - * *variable, there's a useful assignment - * shortcut for extracting matched fields. This is slightly slower than - * accessing the fields directly (as an intermediate array is - * generated). - * - * all,f1,f2,f3 = * /(.)(.)(\d+)(\d)/.match("THX1138.") - * all #=> "HX1138" - * f1 #=> "H" - * f2 #=> "X" - * f3 #=> "113" */ static VALUE @@ -2030,15 +2035,18 @@ match_to_a(VALUE match) /* * call-seq: - * mtch.captures -> array + * captures -> array + * + * Returns the array of captures, + * which are all matches except m[0]: + * + * m = /(.)(.)(\d+)(\d)/.match("THX1138.") + * # => # + * m[0] # => "HX1138" + * m.captures # => ["H", "X", "113", "8"] * - * Returns the array of captures; equivalent to mtch.to_a[1..-1]. + * Related: MatchData.to_a. * - * f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures - * f1 #=> "H" - * f2 #=> "X" - * f3 #=> "113" - * f4 #=> "8" */ static VALUE match_captures(VALUE match) -- cgit v1.2.1