[Bug #18998] Honor `#to_str` next to `#to_int` in `Kernel#Integer` · ruby/ruby@7563604 · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit 7563604

Browse files
committed
[Bug #18998] Honor #to_str next to #to_int in Kernel#Integer
1 parent 4f1e0bf commit 7563604

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,9 @@ rb_convert_to_integer(VALUE val, int base, int raise_exception)
31383138
tmp = rb_protect(rb_check_to_int, val, NULL);
31393139
if (RB_INTEGER_TYPE_P(tmp)) return tmp;
31403140
rb_set_errinfo(Qnil);
3141+
if (!NIL_P(tmp = rb_check_string_type(val))) {
3142+
return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
3143+
}
31413144

31423145
if (!raise_exception) {
31433146
VALUE result = rb_protect(rb_check_to_i, val, NULL);

test/ruby/test_integer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ class Integer;def method_missing(*);"";end;end
247247
end;
248248
end
249249

250+
def test_Integer_when_to_str
251+
def (obj = Object.new).to_str
252+
"0x10"
253+
end
254+
assert_equal(16, Integer(obj))
255+
end
256+
250257
def test_int_p
251258
assert_not_predicate(1.0, :integer?)
252259
assert_predicate(1, :integer?)

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.