diff --git a/lib/matrix.rb b/lib/matrix.rb index 30f9f81..66107ff 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1457,25 +1457,16 @@ class Matrix 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