lib/code/object/decimal.rb in code-ruby-0.2.4 vs lib/code/object/decimal.rb in code-ruby-0.3.0

- old
+ new

@@ -17,14 +17,18 @@ def call(**args) operator = args.fetch(:operator, nil) arguments = args.fetch(:arguments, []) - if %w[% - + / * **].detect { |o| operator == o } + if %w[% - / * **].detect { |o| operator == o } number_operation(operator.to_sym, arguments) elsif %w[< <= > >=].detect { |o| operator == o } comparaison(operator.to_sym, arguments) + elsif %w[<< >> & | ^].detect { |o| operator == o } + integer_operation(operator.to_sym, arguments) + elsif operator == "+" + plus(arguments) else super end end @@ -42,13 +46,30 @@ sig(arguments, ::Code::Object::Number) other = arguments.first.value ::Code::Object::Decimal.new(raw.public_send(operator, other.raw)) end + def integer_operation(operator, arguments) + sig(arguments, ::Code::Object::Number) + other = arguments.first.value + ::Code::Object::Integer.new(raw.to_i.public_send(operator, other.raw.to_i)) + end + def comparaison(operator, arguments) sig(arguments, ::Code::Object::Number) other = arguments.first.value ::Code::Object::Boolean.new(raw.public_send(operator, other.raw)) + end + + def plus(arguments) + sig(arguments, ::Code::Object) + other = arguments.first.value + + if other.is_a?(::Code::Object::Number) + ::Code::Object::Decimal.new(raw + other.raw) + else + ::Code::Object::String.new(to_s + other.to_s) + end end end end end