Project

General

Profile

Actions

Bug #12485

closed

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

Added by mrkn (Kenta Murata) over 8 years ago. Updated about 4 years ago.

Status:
Closed
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 8 years ago

Attached patch can fix this issue.

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

You can use RB_INTEGER_TYPE_P in the trunk.

Updated by mrkn (Kenta Murata) over 8 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 5 years 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) about 4 years ago

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

Updated by Anonymous about 4 years ago

  • Status changed from Assigned to Closed

Applied in changeset git|d23d5c3130a0944e7e591370cbf8599009318a7c.


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

[Bug #12485]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0