aboutsummaryrefslogtreecommitdiffstats
path: root/re.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-20 17:14:01 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-09-20 17:14:01 +0000
commit5376745fb65804a5dafe8f13a5bc9a4ced5c263b (patch)
tree8333bf73566d95805e169ac2474d5b76d0702cd9 /re.c
parentf4d9d3d39b2018ea5330c1b21db722cd29ca11b4 (diff)
downloadruby-5376745fb65804a5dafe8f13a5bc9a4ced5c263b.tar.gz
* re.c (rb_reg_match_m): evaluate a block if match. it would make
condition statement much shorter, if no else clause is needed. * string.c (rb_str_match_m): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13475 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/re.c b/re.c
index 32c8942ef2..871ee60738 100644
--- a/re.c
+++ b/re.c
@@ -1797,6 +1797,19 @@ rb_reg_match2(VALUE re)
*
* /(.)(.)(.)/.match("abc")[2] #=> "b"
* /(.)(.)/.match("abc", 1)[2] #=> "c"
+ *
+ * If a block is given, invoke the block with MatchData if match succeed, so
+ * that you can write
+ *
+ * pat.match(str) {|m| ...}
+ *
+ * instead of
+ *
+ * if m = pat.match(str)
+ * ...
+ * end
+ *
+ * The retuen value is a value from block exection in this case.
*/
static VALUE
@@ -1819,6 +1832,9 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
}
result = rb_backref_get();
rb_match_busy(result);
+ if (!NIL_P(result) && rb_block_given_p()) {
+ return rb_yield(result);
+ }
return result;
}