[前][次][番号順一覧][スレッド一覧]

ruby-changes:41792

From: nobu <ko1@a...>
Date: Thu, 18 Feb 2016 21:05:55 +0900 (JST)
Subject: [ruby-changes:41792] nobu:r53866 (trunk): string.c: Symbol#match

nobu	2016-02-18 21:06:20 +0900 (Thu, 18 Feb 2016)

  New Revision: 53866

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53866

  Log:
    string.c: Symbol#match
    
    * string.c (sym_match_m): delegate to String#match but not
      String#=~.  [ruby-core:72864] [Bug #11991]

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_symbol.rb
Index: string.c
===================================================================
--- string.c	(revision 53865)
+++ string.c	(revision 53866)
@@ -9423,7 +9423,6 @@ sym_casecmp(VALUE sym, VALUE other) https://github.com/ruby/ruby/blob/trunk/string.c#L9423
 /*
  * call-seq:
  *   sym =~ obj   -> fixnum or nil
- *   sym.match(obj)   -> fixnum or nil
  *
  * Returns <code>sym.to_s =~ obj</code>.
  */
@@ -9436,6 +9435,19 @@ sym_match(VALUE sym, VALUE other) https://github.com/ruby/ruby/blob/trunk/string.c#L9435
 
 /*
  * call-seq:
+ *   sym.match(obj)   -> MatchData or nil
+ *
+ * Returns <code>sym.to_s.match(obj)</code>.
+ */
+
+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
  *   sym[b, n]     -> string
  *   sym.slice(idx)      -> char
@@ -9763,7 +9775,7 @@ Init_String(void) https://github.com/ruby/ruby/blob/trunk/string.c#L9775
     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);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53865)
+++ ChangeLog	(revision 53866)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 18 21:05:47 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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  <her@s...>
 
 	* re.c: Add MatchData#named_captures
Index: test/ruby/test_symbol.rb
===================================================================
--- test/ruby/test_symbol.rb	(revision 53865)
+++ test/ruby/test_symbol.rb	(revision 53866)
@@ -246,6 +246,30 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L246
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]