From 902b2ce3c669d4e3b5c20b2c90a3b3982f17d820 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Mon, 27 May 2019 21:29:49 +0900 Subject: Fix `Rational#==(Complex)` Consider a Numreic class like `Complex` that does not have `<=>` but `==` works (`0i == 0r` is `true`). --- mrbgems/mruby-rational/mrblib/rational.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'mrbgems/mruby-rational/mrblib') diff --git a/mrbgems/mruby-rational/mrblib/rational.rb b/mrbgems/mruby-rational/mrblib/rational.rb index 4936b1123..54d9a955f 100644 --- a/mrbgems/mruby-rational/mrblib/rational.rb +++ b/mrbgems/mruby-rational/mrblib/rational.rb @@ -62,6 +62,19 @@ class Rational < Numeric nil end end + + def ==(rhs) + if rhs.is_a?(Integral) + return numerator == rhs if denominator == 1 + rhs = Rational(rhs) + end + + if rhs.is_a?(Rational) + numerator * rhs.denominator == denominator * rhs.numerator + else + rhs == self + end + end end class Numeric -- cgit v1.2.3