Class: Code::Object::Decimal
- Inherits:
-
Number
- Object
- Code::Object
- Number
- Code::Object::Decimal
- Defined in:
- lib/code/object/decimal.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
- #call(**args) ⇒ Object
-
#initialize(decimal, exponent: nil) ⇒ Decimal
constructor
A new instance of Decimal.
- #inspect ⇒ Object
- #to_s ⇒ Object
Methods inherited from Code::Object
#<=>, #==, #falsy?, #hash, #truthy?
Constructor Details
#initialize(decimal, exponent: nil) ⇒ Decimal
Returns a new instance of Decimal.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/code/object/decimal.rb', line 6 def initialize(decimal, exponent: nil) @raw = BigDecimal(decimal) if exponent if exponent.is_a?(::Code::Object::Number) @raw = @raw * 10**exponent.raw else raise ::Code::Error::TypeError.new("exponent is not a number") end end end |
Instance Attribute Details
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
4 5 6 |
# File 'lib/code/object/decimal.rb', line 4 def raw @raw end |
Instance Method Details
#call(**args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/code/object/decimal.rb', line 18 def call(**args) operator = args.fetch(:operator, nil) arguments = args.fetch(:arguments, []) value = arguments.first&.value if operator == "%" sig(arguments) { ::Code::Object::Number } modulo(value) elsif operator == "+" if value sig(arguments) { ::Code::Object } plus(value) else sig(arguments) self end elsif operator == "-" if value sig(arguments) { ::Code::Object::Number } minus(value) else sig(arguments) unary_minus end elsif operator == "/" sig(arguments) { ::Code::Object::Number } division(value) elsif operator == "*" sig(arguments) { ::Code::Object::Number } multiplication(value) elsif operator == "**" sig(arguments) { ::Code::Object::Number } power(value) elsif operator == "<" sig(arguments) { ::Code::Object::Number } inferior(value) elsif operator == "<=" sig(arguments) { ::Code::Object::Number } inferior_or_equal(value) elsif operator == ">" sig(arguments) { ::Code::Object::Number } superior(value) elsif operator == ">=" sig(arguments) { ::Code::Object::Number } superior_or_equal(value) elsif operator == "<<" sig(arguments) { ::Code::Object::Number } left_shift(value) elsif operator == ">>" sig(arguments) { ::Code::Object::Number } right_shift(value) elsif operator == "&" sig(arguments) { ::Code::Object::Number } bitwise_and(value) elsif operator == "|" sig(arguments) { ::Code::Object::Number } bitwise_or(value) elsif operator == "^" sig(arguments) { ::Code::Object::Number } bitwise_xor(value) else super end end |
#inspect ⇒ Object
87 88 89 |
# File 'lib/code/object/decimal.rb', line 87 def inspect to_s end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/code/object/decimal.rb', line 83 def to_s raw.to_s("F") end |