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

ruby-changes:74136

From: Nobuyoshi <ko1@a...>
Date: Thu, 20 Oct 2022 15:35:54 +0900 (JST)
Subject: [ruby-changes:74136] 7563604fb8 (master): [Bug #18998] Honor `#to_str` next to `#to_int` in `Kernel#Integer`

https://git.ruby-lang.org/ruby.git/commit/?id=7563604fb8

From 7563604fb868d87057733f52d780d841fc1ab6bb Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 20 Oct 2022 14:07:35 +0900
Subject: [Bug #18998] Honor `#to_str` next to `#to_int` in `Kernel#Integer`

---
 object.c                  | 3 +++
 test/ruby/test_integer.rb | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/object.c b/object.c
index d0c0e34f82..0d99c6db47 100644
--- a/object.c
+++ b/object.c
@@ -3138,6 +3138,9 @@ rb_convert_to_integer(VALUE val, int base, int raise_exception) https://github.com/ruby/ruby/blob/trunk/object.c#L3138
     tmp = rb_protect(rb_check_to_int, val, NULL);
     if (RB_INTEGER_TYPE_P(tmp)) return tmp;
     rb_set_errinfo(Qnil);
+    if (!NIL_P(tmp = rb_check_string_type(val))) {
+        return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
+    }
 
     if (!raise_exception) {
         VALUE result = rb_protect(rb_check_to_i, val, NULL);
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb
index c3e11498be..886888e7b0 100644
--- a/test/ruby/test_integer.rb
+++ b/test/ruby/test_integer.rb
@@ -247,6 +247,13 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L247
     end;
   end
 
+  def test_Integer_when_to_str
+    def (obj = Object.new).to_str
+      "0x10"
+    end
+    assert_equal(16, Integer(obj))
+  end
+
   def test_int_p
     assert_not_predicate(1.0, :integer?)
     assert_predicate(1, :integer?)
-- 
cgit v1.2.3


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

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