mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
f4d9d3d39b
commit
5376745fb6
3 changed files with 42 additions and 2 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Fri Sep 21 02:11:22 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Fri Sep 21 02:02:34 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* hash.c (hash_equal): should call rb_eql when argument eql is set.
|
* hash.c (hash_equal): should call rb_eql when argument eql is set.
|
||||||
|
|
16
re.c
16
re.c
|
@ -1797,6 +1797,19 @@ rb_reg_match2(VALUE re)
|
||||||
*
|
*
|
||||||
* /(.)(.)(.)/.match("abc")[2] #=> "b"
|
* /(.)(.)(.)/.match("abc")[2] #=> "b"
|
||||||
* /(.)(.)/.match("abc", 1)[2] #=> "c"
|
* /(.)(.)/.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
|
static VALUE
|
||||||
|
@ -1819,6 +1832,9 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
|
||||||
}
|
}
|
||||||
result = rb_backref_get();
|
result = rb_backref_get();
|
||||||
rb_match_busy(result);
|
rb_match_busy(result);
|
||||||
|
if (!NIL_P(result) && rb_block_given_p()) {
|
||||||
|
return rb_yield(result);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
string.c
21
string.c
|
@ -1547,17 +1547,34 @@ static VALUE get_pat(VALUE, int);
|
||||||
* 'hello'.match('(.)\1')[0] #=> "ll"
|
* 'hello'.match('(.)\1')[0] #=> "ll"
|
||||||
* 'hello'.match(/(.)\1/)[0] #=> "ll"
|
* 'hello'.match(/(.)\1/)[0] #=> "ll"
|
||||||
* 'hello'.match('xx') #=> nil
|
* '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
|
static VALUE
|
||||||
rb_str_match_m(int argc, VALUE *argv, VALUE str)
|
rb_str_match_m(int argc, VALUE *argv, VALUE str)
|
||||||
{
|
{
|
||||||
VALUE re;
|
VALUE re, result;
|
||||||
if (argc < 1)
|
if (argc < 1)
|
||||||
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
|
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
|
||||||
re = argv[0];
|
re = argv[0];
|
||||||
argv[0] = str;
|
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
|
static int
|
||||||
|
|
Loading…
Add table
Reference in a new issue