Project

General

Profile

Bug #12485

Kernel.Rational raises TypeError though given denominator returns 1 by to_int

Added by mrkn (Kenta Murata) over 3 years ago. Updated about 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
[ruby-core:75995]

Description

Is it an intentional behavior?

class << (o = Object.new)
  def to_int
    1
  end
end

Rational(1, o)  #=> TypeError: not an integer

Files

rational.c.diff (539 Bytes) rational.c.diff mrkn (Kenta Murata), 06/13/2016 02:53 PM
rational-denom-to_int.patch (1.73 KB) rational-denom-to_int.patch jeremyevans0 (Jeremy Evans), 07/25/2019 03:28 PM

History

Updated by mrkn (Kenta Murata) over 3 years ago

Attached patch can fix this issue.

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

You can use RB_INTEGER_TYPE_P in the trunk.

Updated by mrkn (Kenta Murata) over 3 years ago

Nobuyoshi Nakada wrote:

You can use RB_INTEGER_TYPE_P in the trunk.

I forgot it. Thanks.

Updated by jeremyevans0 (Jeremy Evans) about 2 months ago

I'm not sure if this is considered a bug or not. Rational will already try to convert numerator and denominator via to_r. However, if we do want to convert via to_int if to_r is not defined, the attached patch should implement it.

Also available in: Atom PDF