Class: ROdds::Odd::Decimal

Inherits:
Base
  • Object
show all
Defined in:
lib/r_odds/odds/decimal.rb

Instance Method Summary collapse

Constructor Details

#initialize(value:) ⇒ Decimal

Returns a new instance of Decimal.



5
6
7
8
9
10
11
# File 'lib/r_odds/odds/decimal.rb', line 5

def initialize(value:)
  if value.kind_of? Rational
    @value = value
  else
    raise ArgumentError, "Decimal odds must be initialized a Rational instance. Given a #{value.class} instance instead"
  end
end

Instance Method Details

#as_rationalObject



21
22
23
# File 'lib/r_odds/odds/decimal.rb', line 21

def as_rational
  value.rationalize
end

#formatObject



13
14
15
# File 'lib/r_odds/odds/decimal.rb', line 13

def format
  :decimal
end

#to_s(round_to: 2) ⇒ Object



17
18
19
# File 'lib/r_odds/odds/decimal.rb', line 17

def to_s(round_to: 2)
  "%.#{round_to}f" % value
end