summaryrefslogtreecommitdiff
path: root/string.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 /string.c
parentf4d9d3d39b2018ea5330c1b21db722cd29ca11b4 (diff)
* 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 'string.c')
-rw-r--r--string.c21
1 files changed, 19 insertions, 2 deletions
diff --git a/string.c b/string.c
index 0a50f174fe..1cad0f8bf5 100644
--- a/string.c
+++ b/string.c
@@ -1547,17 +1547,34 @@ static VALUE get_pat(VALUE, int);
* 'hello'.match('(.)\1')[0] #=> "ll"
* 'hello'.match(/(.)\1/)[0] #=> "ll"
* 'hello'.match('xx') #=> nil
+ *
+ * If a block is given, invoke the block with MatchData if match succeed, so
+ * that you can write
+ *
+ * str.match(pat) {|m| ...}
+ *
+ * instead of
+ *
+ * if m = str.match(pat)
+ * ...
+ * end
+ *
+ * The retuen value is a value from block exection in this case.
*/
static VALUE
rb_str_match_m(int argc, VALUE *argv, VALUE str)
{
- VALUE re;
+ VALUE re, result;
if (argc < 1)
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
re = argv[0];
argv[0] = str;
- return rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv);
+ result = rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv);
+ if (!NIL_P(result) && rb_block_given_p()) {
+ return rb_yield(result);
+ }
+ return result;
}
static int