[ruby-core:111857] [Ruby master Bug#19349] Kernel.Integer silently discards base argument that does not have a #to_int

Issue #19349 has been reported by herwin (Herwin W). ---------------------------------------- Bug #19349: Kernel.Integer silently discards base argument that does not have a #to_int https://bugs.ruby-lang.org/issues/19349 * Author: herwin (Herwin W) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- The method `Kernel.Integer` has an optional argument `base`, which is the base to convert the input to: ``` irb(main):001:0> Kernel.Integer('10', 8) => 8 ``` If the base argument is not an integer, it is converted using `#to_int` ``` irb(main):002:0> Kernel.Integer('10', 8.2) => 8 irb(main):003:0> Kernel.Integer('10', Object.new.tap { |x| x.define_singleton_method(:to_int) { 8 } }) => 8 ``` But if the base argument is not an Integer, and does not have a `#to_int` method, it gets discarded ``` irb(main):004:0> Kernel.Integer('10', '8') => 10 ``` I would expect this to raise a `TypeError`, similar to `String#to_i`: ``` irb(main):005:0> '10'.to_i('8') (irb):5:in `to_i': no implicit conversion of String into Integer (TypeError) ``` This has briefly been discussed on the ruby-spec github repo, and they suggested to report this as a bug. (see https://github.com/ruby/spec/pull/997#issuecomment-1382825900) -- https://bugs.ruby-lang.org/

Issue #19349 has been updated by herwin (Herwin W). And likely related: in case the output of the call to `to_int` returns something that is not an integer, it gets discarded as well ``` Kernel.Integer('10', Object.new.tap { |x| x.define_singleton_me thod(:to_int) { '8' } }) => 10 ``` ---------------------------------------- Bug #19349: Kernel.Integer silently discards base argument that does not have a #to_int https://bugs.ruby-lang.org/issues/19349#change-101271 * Author: herwin (Herwin W) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- The method `Kernel.Integer` has an optional argument `base`, which is the base to convert the input to: ``` irb(main):001:0> Kernel.Integer('10', 8) => 8 ``` If the base argument is not an integer, it is converted using `#to_int` ``` irb(main):002:0> Kernel.Integer('10', 8.2) => 8 irb(main):003:0> Kernel.Integer('10', Object.new.tap { |x| x.define_singleton_method(:to_int) { 8 } }) => 8 ``` But if the base argument is not an Integer, and does not have a `#to_int` method, it gets discarded ``` irb(main):004:0> Kernel.Integer('10', '8') => 10 ``` I would expect this to raise a `TypeError`, similar to `String#to_i`: ``` irb(main):005:0> '10'.to_i('8') (irb):5:in `to_i': no implicit conversion of String into Integer (TypeError) ``` This has briefly been discussed on the ruby-spec github repo, and they suggested to report this as a bug. (see https://github.com/ruby/spec/pull/997#issuecomment-1382825900) -- https://bugs.ruby-lang.org/

Issue #19349 has been updated by nobu (Nobuyoshi Nakada). https://github.com/ruby/ruby/pull/7144 ---------------------------------------- Bug #19349: Kernel.Integer silently discards base argument that does not have a #to_int https://bugs.ruby-lang.org/issues/19349#change-101281 * Author: herwin (Herwin W) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- The method `Kernel.Integer` has an optional argument `base`, which is the base to convert the input to: ``` irb(main):001:0> Kernel.Integer('10', 8) => 8 ``` If the base argument is not an integer, it is converted using `#to_int` ``` irb(main):002:0> Kernel.Integer('10', 8.2) => 8 irb(main):003:0> Kernel.Integer('10', Object.new.tap { |x| x.define_singleton_method(:to_int) { 8 } }) => 8 ``` But if the base argument is not an Integer, and does not have a `#to_int` method, it gets discarded ``` irb(main):004:0> Kernel.Integer('10', '8') => 10 ``` I would expect this to raise a `TypeError`, similar to `String#to_i`: ``` irb(main):005:0> '10'.to_i('8') (irb):5:in `to_i': no implicit conversion of String into Integer (TypeError) ``` This has briefly been discussed on the ruby-spec github repo, and they suggested to report this as a bug. (see https://github.com/ruby/spec/pull/997#issuecomment-1382825900) -- https://bugs.ruby-lang.org/
participants (2)
-
herwin (Herwin W)
-
nobu (Nobuyoshi Nakada)