diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-03-18 16:27:35 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-03-19 10:37:10 +0900 |
| commit | 08f9d5bab5092fe1193dd100765079e3e4d746b9 (patch) | |
| tree | 627588dd204f5a7c24ea79ab17076f3eeab818eb /mrbgems/mruby-rational/mrblib | |
| parent | eb070303080b24458d3740424aa7aa8a45fa49eb (diff) | |
| download | mruby-08f9d5bab5092fe1193dd100765079e3e4d746b9.tar.gz mruby-08f9d5bab5092fe1193dd100765079e3e4d746b9.zip | |
rational.c: overhaul rational operators.
- define `MRB_TT_RATIONAL`
- change object structure (`struct RRational`)
- add memory management for `MRB_TT_RATIONAL`
- avoid operator overloading as much as possible
- implement division overloading in C
- as a result, performance improved a lot
Diffstat (limited to 'mrbgems/mruby-rational/mrblib')
| -rw-r--r-- | mrbgems/mruby-rational/mrblib/rational.rb | 36 |
1 files changed, 0 insertions, 36 deletions
diff --git a/mrbgems/mruby-rational/mrblib/rational.rb b/mrbgems/mruby-rational/mrblib/rational.rb index aaafc9899..a9edeaad4 100644 --- a/mrbgems/mruby-rational/mrblib/rational.rb +++ b/mrbgems/mruby-rational/mrblib/rational.rb @@ -64,17 +64,6 @@ class Rational < Numeric nil end end - - def ==(rhs) - return true if self.equal?(rhs) - case rhs - when Integer, Float - return numerator == rhs if denominator == 1 - when Rational - return numerator * rhs.denominator == denominator * rhs.numerator - end - rhs == self - end end class Numeric @@ -82,28 +71,3 @@ class Numeric Rational(self, 1) end end - -module Kernel - [:+, :-, :*, :/, :<=>, :==, :<, :<=, :>, :>=].each do |op| - original_operator_name = :"__original_operator_#{op}_rational" - Integer.instance_eval do - alias_method original_operator_name, op - define_method op do |rhs| - if rhs.is_a? Rational - Rational(self).__send__(op, rhs) - else - __send__(original_operator_name, rhs) - end - end - end - Float.instance_eval do - alias_method original_operator_name, op - define_method op do |rhs| - if rhs.is_a? Rational - rhs = rhs.to_f - end - __send__(original_operator_name, rhs) - end - end if Object.const_defined?(:Float) - end -end |
