diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-02-05 21:43:15 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-02-05 21:54:11 +0900 |
| commit | c6149370b05d6e775022501a607055c44b01cf5a (patch) | |
| tree | 23982885a839a1b5426e287f954cd4cb0436ba45 /mrbgems/mruby-complex | |
| parent | 7ce03e32b7b21ae7f095dbc23b23497143919b01 (diff) | |
| download | mruby-c6149370b05d6e775022501a607055c44b01cf5a.tar.gz mruby-c6149370b05d6e775022501a607055c44b01cf5a.zip | |
Refactor method overriding.
* Use `class_eval` instead of `instance_eval`.
* Reduce `class_eval` calls
* define `Numeric#i`
* undefine `Complex#i`
Diffstat (limited to 'mrbgems/mruby-complex')
| -rw-r--r-- | mrbgems/mruby-complex/mrblib/complex.rb | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb index 26a3b206f..f54151c7b 100644 --- a/mrbgems/mruby-complex/mrblib/complex.rb +++ b/mrbgems/mruby-complex/mrblib/complex.rb @@ -109,8 +109,8 @@ class Complex < Numeric alias_method :imag, :imaginary [Integer, Float].each do |cls| - [:+, :-, :*, :/, :==].each do |op| - cls.instance_eval do + cls.class_eval do + [:+, :-, :*, :/, :==].each do |op| original_operator_name = :"__original_operator_#{op}_complex" alias_method original_operator_name, op define_method op do |rhs| @@ -123,4 +123,10 @@ class Complex < Numeric end end end + Numeric.class_eval do + def i + Complex(0, self) + end + end + undef i end |
