From 49a272d728c2003064182869cfdbca140f3acc81 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 18 Feb 2016 12:06:20 +0000 Subject: [PATCH] string.c: Symbol#match * string.c (sym_match_m): delegate to String#match but not String#=~. [ruby-core:72864] [Bug #11991] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53866 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ string.c | 16 ++++++++++++++-- test/ruby/test_symbol.rb | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index b3124dcc09..08dfc459f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Feb 18 21:05:47 2016 Nobuyoshi Nakada + + * string.c (sym_match_m): delegate to String#match but not + String#=~. [ruby-core:72864] [Bug #11991] + Thu Feb 18 14:15:38 2016 Shota Fukumori * re.c: Add MatchData#named_captures diff --git a/string.c b/string.c index 7850bcd1d5..2782d28e52 100644 --- a/string.c +++ b/string.c @@ -9423,7 +9423,6 @@ sym_casecmp(VALUE sym, VALUE other) /* * call-seq: * sym =~ obj -> fixnum or nil - * sym.match(obj) -> fixnum or nil * * Returns sym.to_s =~ obj. */ @@ -9434,6 +9433,19 @@ sym_match(VALUE sym, VALUE other) return rb_str_match(rb_sym2str(sym), other); } +/* + * call-seq: + * sym.match(obj) -> MatchData or nil + * + * Returns sym.to_s.match(obj). + */ + +static VALUE +sym_match_m(int argc, VALUE *argv, VALUE sym) +{ + return rb_str_match_m(argc, argv, rb_sym2str(sym)); +} + /* * call-seq: * sym[idx] -> char @@ -9763,7 +9775,7 @@ Init_String(void) rb_define_method(rb_cSymbol, "length", sym_length, 0); rb_define_method(rb_cSymbol, "size", sym_length, 0); rb_define_method(rb_cSymbol, "empty?", sym_empty, 0); - rb_define_method(rb_cSymbol, "match", sym_match, 1); + rb_define_method(rb_cSymbol, "match", sym_match_m, -1); rb_define_method(rb_cSymbol, "upcase", sym_upcase, -1); rb_define_method(rb_cSymbol, "downcase", sym_downcase, -1); diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index 5d42a5f92d..7ed478864c 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -246,6 +246,30 @@ class TestSymbol < Test::Unit::TestCase assert_equal(:fOo, :FoO.swapcase) end + def test_MATCH # '=~' + assert_equal(10, :"FeeFieFoo-Fum" =~ /Fum$/) + assert_equal(nil, "FeeFieFoo-Fum" =~ /FUM$/) + + o = Object.new + def o.=~(x); x + "bar"; end + assert_equal("foobar", :"foo" =~ o) + + assert_raise(TypeError) { :"foo" =~ "foo" } + end + + def test_match_method + assert_equal("bar", :"foobarbaz".match(/bar/).to_s) + + o = Regexp.new('foo') + def o.match(x, y, z); x + y + z; end + assert_equal("foobarbaz", :"foo".match(o, "bar", "baz")) + x = nil + :"foo".match(o, "bar", "baz") {|y| x = y } + assert_equal("foobarbaz", x) + + assert_raise(ArgumentError) { :"foo".match } + end + def test_symbol_poped assert_nothing_raised { eval('a = 1; :"#{ a }"; 1') } end