Refactor method overriding.

* Use `class_eval` instead of `instance_eval`.
* Reduce `class_eval` calls
* define `Numeric#i`
* undefine `Complex#i`
parent 7ce03e32
...@@ -109,8 +109,8 @@ class Complex < Numeric ...@@ -109,8 +109,8 @@ class Complex < Numeric
alias_method :imag, :imaginary alias_method :imag, :imaginary
[Integer, Float].each do |cls| [Integer, Float].each do |cls|
cls.class_eval do
[:+, :-, :*, :/, :==].each do |op| [:+, :-, :*, :/, :==].each do |op|
cls.instance_eval do
original_operator_name = :"__original_operator_#{op}_complex" original_operator_name = :"__original_operator_#{op}_complex"
alias_method original_operator_name, op alias_method original_operator_name, op
define_method op do |rhs| define_method op do |rhs|
...@@ -123,4 +123,10 @@ class Complex < Numeric ...@@ -123,4 +123,10 @@ class Complex < Numeric
end end
end end
end end
Numeric.class_eval do
def i
Complex(0, self)
end
end
undef i
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment