Class: FixedOdds

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/FixedOdds.rb

Overview

Represents fixed odds used in gambling and betting. Supports fractional, moneyline and decimal representations. Can calculate the profit and return on a winning bet.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fractional_odds) ⇒ FixedOdds

creates a new FixedOdds from a Rational

Parameters:

  • fractional_odds (Rational)

    the odds



69
70
71
# File 'lib/FixedOdds.rb', line 69

def initialize(fractional_odds)
  @fractional_odds = fractional_odds
end

Instance Attribute Details

#fractional_oddsObject (readonly)

the fractional odds as a Rational



10
11
12
# File 'lib/FixedOdds.rb', line 10

def fractional_odds
  @fractional_odds
end

Class Method Details

.decimal_odds(decimal) ⇒ FixedOdds

creates a new FixedOdds from decimal form. Examples are

  • 1.25

  • 2

Parameters:

  • decimal (String)

    odds in decimal form

Returns:



92
93
94
95
# File 'lib/FixedOdds.rb', line 92

def FixedOdds.decimal_odds(decimal)
  raise %{could not parse "#{decimal}" as decimal odds} unless self.decimal_odds?(decimal)
  new(Rational(decimal.to_f - 1))
end

.decimal_odds?(odds) ⇒ Boolean

tells if the odds are in decimal form

Parameters:

  • odds (String)

    the odds representation

Returns:

  • (Boolean)

    to indicate if it matches



44
45
46
# File 'lib/FixedOdds.rb', line 44

def FixedOdds.decimal_odds?(odds)
  odds =~ /^(\d+|\d+\.\d+|\.\d+)$/ 
end

.fractional_odds(fractional) ⇒ FixedOdds

creates a new FixedOdds from fractional form. These can be in the form

  • 4-to-1

  • 4-to-1 against

  • 4-to-1 on

  • 4/1

  • 4/1 against

  • 4/1 on

  • evens

  • even money

Parameters:

  • fractional (String)

    odds in fractional form

Returns:



59
60
61
62
63
64
65
# File 'lib/FixedOdds.rb', line 59

def FixedOdds.fractional_odds(fractional)
  raise %{could not parse "#{fractional}" as fractional odds} unless self.fractional_odds?(fractional)
  return new(Rational('1/1')) if fractional == 'evens' || fractional == 'even money' 
  if /(?<numerator>\d+)(\/|-to-)(?<denominator>\d+)/ =~ fractional then r = Rational("#{numerator}/#{denominator}") end
  r = r.reciprocal if fractional.end_with? ' on'
  new(Rational(r))
end

.fractional_odds?(odds) ⇒ Boolean

tells if the odds are in fractional form

Parameters:

  • odds (String)

    the odds representation

Returns:

  • (Boolean)

    to indicate if it matches



30
31
32
# File 'lib/FixedOdds.rb', line 30

def FixedOdds.fractional_odds?(odds)
  odds =~ /^(\d+(\/|-to-)\d+( (against|on))?|evens|even money)$/
end

.from_s(odds) ⇒ FixedOdds

Note:

strings like ‘5’ are parsed as decimal odds, not as being

creates a new FixedOdds from a string which can be in fractional, moneyline or decimal format equivalent to ‘5/1’

Parameters:

  • odds (String)

    the odds in fractional, moneyline or decimal form

Returns:



18
19
20
21
22
23
24
25
# File 'lib/FixedOdds.rb', line 18

def FixedOdds.from_s(odds)
  case
  when self.fractional_odds?(odds) then self.fractional_odds odds
  when self.moneyline_odds?(odds)  then self.moneyline_odds odds
  when self.decimal_odds?(odds)    then self.decimal_odds odds
  else                             raise ArgumentError, %{could not parse "#{odds}"}
  end
end

.moneyline_odds(moneyline) ⇒ FixedOdds

creates a new FixedOdds from moneyline form. Examples are

  • +400

  • -500

Parameters:

  • moneyline (String)

    odds in moneyline form

Returns:



79
80
81
82
83
84
85
# File 'lib/FixedOdds.rb', line 79

def FixedOdds.moneyline_odds(moneyline)
  raise %{could not parse "#{moneyline}" as moneyline odds} unless self.moneyline_odds?(moneyline)
  sign = moneyline[0]
  if sign == '+' then new(Rational("#{moneyline}/100"))
  else                new(Rational("100/#{moneyline.to_i.magnitude}"))
  end
end

.moneyline_odds?(odds) ⇒ Boolean

tells if the odds are in moneyline form

Parameters:

  • odds (String)

    the odds representation

Returns:

  • (Boolean)

    to indicate if it matches



37
38
39
# File 'lib/FixedOdds.rb', line 37

def FixedOdds.moneyline_odds?(odds)
  odds =~ /^[+-]\d+$/ 
end

Instance Method Details

#profit_on_winning_stake(stake) ⇒ Money

calculates the profit won on a winning bet

Parameters:

  • stake (Money)

    the stake

Returns:

  • (Money)

    the profit



100
101
102
# File 'lib/FixedOdds.rb', line 100

def profit_on_winning_stake(stake)
  stake * @fractional_odds
end

#stake_needed_to_win(win) ⇒ Money

calculates the magnitude of the stake needed to win the specified amount in profit

Parameters:

  • win (Money)

    the desired profit

Returns:

  • (Money)

    the stake required to realise that profit on a winning bet



116
117
118
# File 'lib/FixedOdds.rb', line 116

def stake_needed_to_win win
  win / @fractional_odds
end

#to_sString

string representation in fractional form like ‘4/1’

Returns:

  • (String)

    fractional form representation



122
123
124
# File 'lib/FixedOdds.rb', line 122

def to_s
  to_s_fractional
end

#to_s_decimalString

string representation in decimal form

Returns:

  • (String)

    decimal form representation



146
147
148
# File 'lib/FixedOdds.rb', line 146

def to_s_decimal
  '%g' % (fractional_odds + 1)
end

#to_s_fractionalString

string representation in fractional form like ‘4/1’

Returns:

  • (String)

    fractional form representation



128
129
130
# File 'lib/FixedOdds.rb', line 128

def to_s_fractional
  @fractional_odds.to_s
end

#to_s_moneylineString

string representation in moneyline form

Returns:

  • (String)

    moneyline form representation



134
135
136
137
138
139
140
141
142
# File 'lib/FixedOdds.rb', line 134

def to_s_moneyline
  integral_number_with_sign_regex = '%+d'

  if @fractional_odds > 1.0
    integral_number_with_sign_regex % (fractional_odds * 100).to_i
  else
    integral_number_with_sign_regex % (-100.0 / fractional_odds)
  end
end

#total_return_on_winning_stake(stake) ⇒ Money

calculates the total return on a winning bet (which is the profit plus the initial stake)

Parameters:

  • stake (Money)

    the stake

Returns:

  • (Money)

    the total winnings



108
109
110
# File 'lib/FixedOdds.rb', line 108

def total_return_on_winning_stake(stake)
  profit_on_winning_stake(stake) + stake
end