lib/code/object/decimal.rb in code-ruby-0.13.1 vs lib/code/object/decimal.rb in code-ruby-0.14.0
- old
+ new
@@ -7,18 +7,18 @@
decimal = args.first || "0"
exponent = args.second || "0"
decimal = decimal.raw if decimal.is_an?(Object)
exponent = exponent.raw if exponent.is_an?(Object)
@raw = decimal.to_d * 10**exponent.to_d
- rescue FloatDomainError => e
+ rescue FloatDomainError
raise Error, "#{decimal.inspect} * 10**#{exponent.inspect} is invalid"
end
def call(**args)
operator = args.fetch(:operator, nil)
- arguments = args.fetch(:arguments, [])
- value = arguments.first&.value
+ arguments = args.fetch(:arguments, List.new)
+ value = arguments.code_first
case operator.to_s
when "%", "modulo"
sig(args) { Integer | Decimal }
code_modulo(value)
@@ -31,14 +31,14 @@
when "**", "power"
sig(args) { Integer | Decimal }
code_power(value)
when "+", "plus"
sig(args) { Object.maybe }
- value ? code_plus(value) : self
+ arguments.any? ? code_plus(value) : self
when "-", "minus"
sig(args) { (Integer | Decimal).maybe }
- value ? code_minus(value) : code_unary_minus
+ arguments.any? ? code_minus(value) : code_unary_minus
when "/", "division"
sig(args) { Integer | Decimal }
code_division(value)
when "<", "inferior"
sig(args) { Integer | Decimal }
@@ -150,11 +150,11 @@
def code_bitwise_xor(other)
Integer.new(raw.to_i ^ other.raw.to_i)
end
def code_ceil(n = nil)
- n ||= Integer.new(0)
+ n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.ceil(n.raw))
end
def code_clone
Decimal.new(raw)
@@ -175,11 +175,11 @@
def code_five?
Boolean.new(raw == 5)
end
def code_floor(n = nil)
- n ||= Integer.new(0)
+ n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.floor(n.raw))
end
def code_four?
Boolean.new(raw == 4)
@@ -232,11 +232,11 @@
def code_right_shift(other)
Integer.new(raw.to_i >> other.raw.to_i)
end
def code_round(n = nil)
- n ||= Integer.new(0)
+ n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.round(n.raw))
end
def code_seven?
Boolean.new(raw == 7)
@@ -277,11 +277,11 @@
def code_to_string
String.new(raw.to_s("F"))
end
def code_truncate(n = nil)
- n ||= Integer.new(0)
+ n = Integer.new(0) if n.nil? || n.is_a?(Nothing)
Decimal.new(raw.truncate(n.raw))
end
def code_two?
Boolean.new(raw == 2)
@@ -291,21 +291,9 @@
Decimal.new(-raw)
end
def code_zero?
Boolean.new(raw.zero?)
- end
-
- def whole?
- whole == raw
- end
-
- def whole
- raw.round
- end
-
- def as_json(...)
- whole? ? whole.as_json(...) : super
end
end
end
end