Project

General

Profile

Bug #12485

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

Added by mrkn (Kenta Murata) over 4 years ago. Updated 6 days ago.

Status:
Closed
Priority:
Normal
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

Updated by mrkn (Kenta Murata) over 4 years ago

Attached patch can fix this issue.

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

You can use RB_INTEGER_TYPE_P in the trunk.

Updated by mrkn (Kenta Murata) over 4 years ago

Nobuyoshi Nakada wrote:

You can use RB_INTEGER_TYPE_P in the trunk.

I forgot it. Thanks.

Updated by jeremyevans0 (Jeremy Evans) over 1 year 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.

Updated by mrkn (Kenta Murata) 6 days ago

  • Assignee set to mrkn (Kenta Murata)
  • Status changed from Open to Assigned
#6

Updated by Anonymous 6 days ago

  • Status changed from Assigned to Closed

Applied in changeset git|d23d5c3130a0944e7e591370cbf8599009318a7c.


rational.c: try converting by to_int in Rational() (#3684)

[Bug #12485]

Also available in: Atom PDF