diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-05-27 21:29:49 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-05-27 21:29:49 +0900 |
| commit | 902b2ce3c669d4e3b5c20b2c90a3b3982f17d820 (patch) | |
| tree | 67bb12e4a1a389d6d25ac851e32f561b77a12c64 /mrbgems/mruby-rational/mrblib | |
| parent | 8ad777c702b9491e72c37b63ebe7be4ad019ee56 (diff) | |
| download | mruby-902b2ce3c669d4e3b5c20b2c90a3b3982f17d820.tar.gz mruby-902b2ce3c669d4e3b5c20b2c90a3b3982f17d820.zip | |
Fix `Rational#==(Complex)`
Consider a Numreic class like `Complex` that does not have `<=>` but `==`
works (`0i == 0r` is `true`).
Diffstat (limited to 'mrbgems/mruby-rational/mrblib')
| -rw-r--r-- | mrbgems/mruby-rational/mrblib/rational.rb | 13 |
1 files changed, 13 insertions, 0 deletions
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 |
