Class: BigDecimal

Inherits:
Object
  • Object
show all
Defined in:
lib/light/openai/bigdecimal_fallback.rb

Instance Method Summary collapse

Constructor Details

#initialize(value, _precision = 0) ⇒ BigDecimal

Returns a new instance of BigDecimal.



4
5
6
# File 'lib/light/openai/bigdecimal_fallback.rb', line 4

def initialize(value, _precision = 0)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/light/openai/bigdecimal_fallback.rb', line 56

def method_missing(method_name, *args, &block)
  if to_f.respond_to?(method_name)
    to_f.public_send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#*(other) ⇒ Object



48
49
50
# File 'lib/light/openai/bigdecimal_fallback.rb', line 48

def *(other)
  self.class.new(to_f * other.to_f)
end

#+(other) ⇒ Object



40
41
42
# File 'lib/light/openai/bigdecimal_fallback.rb', line 40

def +(other)
  self.class.new(to_f + other.to_f)
end

#-(other) ⇒ Object



44
45
46
# File 'lib/light/openai/bigdecimal_fallback.rb', line 44

def -(other)
  self.class.new(to_f - other.to_f)
end

#/(other) ⇒ Object



52
53
54
# File 'lib/light/openai/bigdecimal_fallback.rb', line 52

def /(other)
  self.class.new(to_f / other.to_f)
end

#==(other) ⇒ Object



34
35
36
37
38
# File 'lib/light/openai/bigdecimal_fallback.rb', line 34

def ==(other)
  to_f == other.to_f
rescue NoMethodError
  false
end

#coerce(other) ⇒ Object



28
29
30
31
32
# File 'lib/light/openai/bigdecimal_fallback.rb', line 28

def coerce(other)
  [self.class.new(other), self]
rescue ArgumentError, TypeError
  [other.to_f, to_f]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/light/openai/bigdecimal_fallback.rb', line 64

def respond_to_missing?(method_name, include_private = false)
  to_f.respond_to?(method_name, include_private) || super
end

#to_fObject



8
9
10
11
12
# File 'lib/light/openai/bigdecimal_fallback.rb', line 8

def to_f
  Float(@value)
rescue ArgumentError
  0.0
end

#to_iObject



18
19
20
# File 'lib/light/openai/bigdecimal_fallback.rb', line 18

def to_i
  to_f.to_i
end

#to_rObject



22
23
24
25
26
# File 'lib/light/openai/bigdecimal_fallback.rb', line 22

def to_r
  @value.to_r
rescue NoMethodError
  Rational(to_f)
end

#to_sObject



14
15
16
# File 'lib/light/openai/bigdecimal_fallback.rb', line 14

def to_s(*)
  @value.to_s
end