Project

General

Profile

Feature #10059 » using_define_method_for_Scalar_arithmetic.patch

gogotanaka (Kazuki Tanaka), 07/18/2014 08:29 AM

View differences:

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
(2-2/3)