summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-02-05 21:43:15 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-02-05 21:54:11 +0900
commitc6149370b05d6e775022501a607055c44b01cf5a (patch)
tree23982885a839a1b5426e287f954cd4cb0436ba45 /mrbgems/mruby-complex
parent7ce03e32b7b21ae7f095dbc23b23497143919b01 (diff)
downloadmruby-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.rb10
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