Class: Code::Object::Decimal

Inherits:
Number show all
Defined in:
lib/code/object/decimal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #[], #[]=, #falsy?, #hash, #key?, #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

#rawObject (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
# File 'lib/code/object/decimal.rb', line 18

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])

  if %w[% - + / * **].detect { |o| operator == o }
    number_operation(operator.to_sym, arguments)
  elsif %w[< <= > >=].detect { |o| operator == o }
    comparaison(operator.to_sym, arguments)
  else
    super
  end
end

#inspectObject



35
36
37
# File 'lib/code/object/decimal.rb', line 35

def inspect
  to_s
end

#to_sObject



31
32
33
# File 'lib/code/object/decimal.rb', line 31

def to_s
  raw.to_s("F")
end