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

ruby-changes:25987

From: nobu <ko1@a...>
Date: Fri, 30 Nov 2012 17:44:16 +0900 (JST)
Subject: [ruby-changes:25987] nobu:r38044 (trunk): string.c: compare with to_str

nobu	2012-11-30 17:43:52 +0900 (Fri, 30 Nov 2012)

  New Revision: 38044

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38044

  Log:
    string.c: compare with to_str
    
    * string.c (rb_str_cmp_m): try to compare with to_str result if
      possible before calling <=> method.  [ruby-core:49279] [Bug #7342]

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38043)
+++ ChangeLog	(revision 38044)
@@ -1,5 +1,8 @@
-Fri Nov 30 17:43:45 2012  Nobuyoshi Nakada  <nobu@r...>
+Fri Nov 30 17:43:50 2012  Nobuyoshi Nakada  <nobu@r...>
 
+	* string.c (rb_str_cmp_m): try to compare with to_str result if
+	  possible before calling <=> method.  [ruby-core:49279] [Bug #7342]
+
 	* string.c (rb_str_cmp_m): use rb_check_funcall instead of respond_to
 	  and call.
 
Index: string.c
===================================================================
--- string.c	(revision 38043)
+++ string.c	(revision 38044)
@@ -2385,9 +2385,9 @@
     int result;
 
     if (!RB_TYPE_P(str2, T_STRING)) {
-	VALUE tmp;
-	if (!rb_respond_to(str2, rb_intern("to_str"))) {
-	    return Qnil;
+	VALUE tmp = rb_check_funcall(str2, rb_intern("to_str"), 0, 0);
+	if (RB_TYPE_P(tmp, T_STRING)) {
+	    result = rb_str_cmp(str1, tmp);
 	}
 	else if ((tmp = rb_check_funcall(str2, rb_intern("<=>"), 1, &str1)) ==
 		 Qundef) {
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 38043)
+++ test/ruby/test_string.rb	(revision 38044)
@@ -170,8 +170,9 @@
 
     o = Object.new
     def o.to_str; "bar"; end
-    assert_nil("foo" <=> o)
+    assert_equal(1, "foo" <=> o)
 
+    class << o;remove_method :to_str;end
     def o.<=>(x); nil; end
     assert_nil("foo" <=> o)
 

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

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