summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--string.c6
-rw-r--r--test/ruby/test_string.rb3
3 files changed, 9 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 1bc049031c..5762c15ec1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Fri Nov 30 17:43:45 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Fri Nov 30 17:43:50 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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.
diff --git a/string.c b/string.c
index c6379bbf5b..f2ee280a75 100644
--- a/string.c
+++ b/string.c
@@ -2385,9 +2385,9 @@ rb_str_cmp_m(VALUE str1, VALUE str2)
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) {
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 6c73a7ccc7..e988f99629 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -170,8 +170,9 @@ class TestString < Test::Unit::TestCase
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)