lib/code/object/decimal.rb in code-ruby-0.4.0 vs lib/code/object/decimal.rb in code-ruby-0.5.0

- old
+ new

@@ -16,56 +16,67 @@ end def call(**args) operator = args.fetch(:operator, nil) arguments = args.fetch(:arguments, []) + value = arguments.first&.value if operator == "%" - sig(arguments, ::Code::Object::Number) - modulo(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + modulo(value) elsif operator == "+" - sig(arguments, ::Code::Object) - plus(arguments.first.value) + if value + sig(arguments) { ::Code::Object } + plus(value) + else + sig(arguments) + self + end elsif operator == "-" - sig(arguments, ::Code::Object::Number) - minus(arguments.first.value) + if value + sig(arguments) { ::Code::Object::Number } + minus(value) + else + sig(arguments) + unary_minus + end elsif operator == "/" - sig(arguments, ::Code::Object::Number) - division(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + division(value) elsif operator == "*" - sig(arguments, ::Code::Object::Number) - multiplication(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + multiplication(value) elsif operator == "**" - sig(arguments, ::Code::Object::Number) - power(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + power(value) elsif operator == "<" - sig(arguments, ::Code::Object::Number) - inferior(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + inferior(value) elsif operator == "<=" - sig(arguments, ::Code::Object::Number) - inferior_or_equal(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + inferior_or_equal(value) elsif operator == ">" - sig(arguments, ::Code::Object::Number) - superior(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + superior(value) elsif operator == ">=" - sig(arguments, ::Code::Object::Number) - superior_or_equal(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + superior_or_equal(value) elsif operator == "<<" - sig(arguments, ::Code::Object::Number) - left_shift(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + left_shift(value) elsif operator == ">>" - sig(arguments, ::Code::Object::Number) - right_shift(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + right_shift(value) elsif operator == "&" - sig(arguments, ::Code::Object::Number) - bitwise_and(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + bitwise_and(value) elsif operator == "|" - sig(arguments, ::Code::Object::Number) - bitwise_or(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + bitwise_or(value) elsif operator == "^" - sig(arguments, ::Code::Object::Number) - bitwise_xor(arguments.first.value) + sig(arguments) { ::Code::Object::Number } + bitwise_xor(value) else super end end @@ -139,9 +150,13 @@ ::Code::Object::Integer.new(raw.to_i | other.raw.to_i) end def bitwise_xor(other) ::Code::Object::Integer.new(raw.to_i ^ other.raw.to_i) + end + + def unary_minus + ::Code::Object::Decimal.new(-raw) end end end end