ruby-changes:1984
From: ko1@a...
Date: 21 Sep 2007 02:14:22 +0900
Subject: [ruby-changes:1984] matz - Ruby:r13475 (trunk): * re.c (rb_reg_match_m): evaluate a block if match. it would make
matz 2007-09-21 02:14:01 +0900 (Fri, 21 Sep 2007)
New Revision: 13475
Modified files:
trunk/ChangeLog
trunk/re.c
trunk/string.c
Log:
* 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.
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/string.c?r1=13475&r2=13474
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13475&r2=13474
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/re.c?r1=13475&r2=13474
Index: re.c
===================================================================
--- re.c (revision 13474)
+++ re.c (revision 13475)
@@ -1797,6 +1797,19 @@
*
* /(.)(.)(.)/.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 @@
}
result = rb_backref_get();
rb_match_busy(result);
+ if (!NIL_P(result) && rb_block_given_p()) {
+ return rb_yield(result);
+ }
return result;
}
Index: ChangeLog
===================================================================
--- ChangeLog (revision 13474)
+++ ChangeLog (revision 13475)
@@ -1,3 +1,10 @@
+Fri Sep 21 02:11:22 2007 Yukihiro Matsumoto <matz@r...>
+
+ * 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.
+
Fri Sep 21 02:02:34 2007 Yukihiro Matsumoto <matz@r...>
* hash.c (hash_equal): should call rb_eql when argument eql is set.
Index: string.c
===================================================================
--- string.c (revision 13474)
+++ string.c (revision 13475)
@@ -1547,17 +1547,34 @@
* '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
--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml