Feature #10059 » using_define_method_for_Scalar_arithmetic.patch
lib/matrix.rb | ||
---|---|---|
end
|
||
# ARITHMETIC
|
||
def +(other)
|
||
case other
|
||
when Numeric
|
||
Scalar.new(@value + other)
|
||
when Vector, Matrix
|
||
Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
|
||
else
|
||
apply_through_coercion(other, __method__)
|
||
end
|
||
end
|
||
def -(other)
|
||
case other
|
||
when Numeric
|
||
Scalar.new(@value - other)
|
||
when Vector, Matrix
|
||
Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
|
||
else
|
||
apply_through_coercion(other, __method__)
|
||
%i(+ -).each do |op|
|
||
define_method(op) do |other|
|
||
case other
|
||
when Numeric
|
||
Scalar.new(@value.send(op, other))
|
||
when Vector, Matrix
|
||
Scalar.Raise ErrOperationNotDefined, op, @value.class, other.class
|
||
else
|
||
apply_through_coercion(other, op)
|
||
end
|
||
end
|
||
end
|
||