class FixedOdds

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

Attributes

fractional_odds[R]

the fractional odds as a Rational

Public Class Methods

decimal_odds(decimal) click to toggle source

creates a new FixedOdds from decimal form. Examples are

  • 1.25

  • 2

@param [String] decimal odds in decimal form @return (see FixedOdds.from_s)

# File lib/fixed_odds.rb, line 98
def FixedOdds.decimal_odds decimal
  raise %{could not parse "#{decimal}" as decimal odds} unless self.decimal_odds?(decimal)
  new(decimal.to_r - 1)
end
decimal_odds?(odds) click to toggle source

tells if the odds are in decimal form @param (see FixedOdds.fractional_odds?) @return (see FixedOdds.fractional_odds?)

# File lib/fixed_odds.rb, line 45
def FixedOdds.decimal_odds? odds
  odds =~ /^([1-9]\d*(\.\d+)?|\.\d+)$/ 
end
fractional_odds(fractional) click to toggle source

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

@param [String] fractional odds in fractional form @return (see FixedOdds.from_s)

# File lib/fixed_odds.rb, line 60
def FixedOdds.fractional_odds fractional
  raise %{could not parse "#{fractional}" as fractional odds} unless self.fractional_odds?(fractional)
  return new(1.to_r) if fractional.inquiry.evens? or fractional == 'even money' 

  o = /(?<numerator>\d+)(\/|-to-)(?<denominator>\d+)/.match fractional
  r = Rational o[:numerator], o[:denominator]
  r = 1 / r if fractional.ends_with? ' on'
  new r
end
fractional_odds?(odds) click to toggle source

tells if the odds are in fractional form @param [String] odds the odds representation @return [Boolean] to indicate if it matches

# File lib/fixed_odds.rb, line 31
def FixedOdds.fractional_odds? odds
  odds =~ /^([1-9]\d*(\/|-to-)[1-9]\d*( (against|on))?|evens|even money)$/
end
from_s(odds) click to toggle source

creates a new FixedOdds from a string which can be in fractional, moneyline or decimal format @note strings like ‘5’ are parsed as decimal odds, not as being equivalent to ‘5/1’ @param [String] odds the odds in fractional, moneyline or decimal form @return [FixedOdds]

# File lib/fixed_odds.rb, line 19
def FixedOdds.from_s odds
  case
  when fractional_odds?(odds) then fractional_odds odds
  when moneyline_odds?(odds)  then moneyline_odds odds
  when decimal_odds?(odds)    then decimal_odds odds
  else                        raise ArgumentError, %{could not parse "#{odds}"}
  end
end
moneyline_odds(moneyline) click to toggle source

creates a new FixedOdds from moneyline form. Examples are

  • +400

  • -500

@note (see from_s) @param [String] moneyline odds in moneyline form @return (see FixedOdds.from_s)

# File lib/fixed_odds.rb, line 82
def FixedOdds.moneyline_odds moneyline
  raise %{could not parse "#{moneyline}" as moneyline odds} unless self.moneyline_odds?(moneyline)

  case moneyline[0]
  when '+'
    new(Rational(moneyline, 100))
  else
    new(Rational(100, -moneyline.to_i))
  end
end
moneyline_odds?(odds) click to toggle source

tells if the odds are in moneyline form @param (see FixedOdds.fractional_odds?) @return (see FixedOdds.fractional_odds?)

# File lib/fixed_odds.rb, line 38
def FixedOdds.moneyline_odds? odds
  odds =~ /^[+-][1-9]\d*$/ 
end
new(fractional_odds) click to toggle source

creates a new FixedOdds from a Rational @param [Rational] fractional_odds the odds

# File lib/fixed_odds.rb, line 72
def initialize fractional_odds
  @fractional_odds = fractional_odds
end

Public Instance Methods

profit_on_stake(stake) click to toggle source

calculates the profit on a winning stake @param [Money] stake the stake @return [Money] the profit

# File lib/fixed_odds.rb, line 106
def profit_on_stake stake
  stake * @fractional_odds
end
stake_to_profit(profit) click to toggle source

calculates the stake needed to win the specified amount in profit @param [Money] profit the desired profit @return [Money] the stake required to realise that profit on a winning bet

# File lib/fixed_odds.rb, line 122
def stake_to_profit profit
  profit / @fractional_odds
end
to_s() click to toggle source

string representation in fractional form like ‘4/1’ @return [String] fractional form representation

# File lib/fixed_odds.rb, line 128
def to_s
  to_s_fractional
end
to_s_decimal() click to toggle source

string representation in decimal form @return [String] decimal form representation

# File lib/fixed_odds.rb, line 149
def to_s_decimal
  '%g' % (fractional_odds + 1)
end
to_s_fractional() click to toggle source

string representation in fractional form like ‘4/1’ @return (see to_s)

# File lib/fixed_odds.rb, line 134
def to_s_fractional
  @fractional_odds.to_s
end
to_s_moneyline() click to toggle source

string representation in moneyline form (note there can be some loss of precision to make the number into an integer) @return [String] moneyline form representation

# File lib/fixed_odds.rb, line 142
def to_s_moneyline
  number = @fractional_odds > 1 ? fractional_odds * 100 : -100 / fractional_odds
  '%+d' % number.round
end
total_return_on_stake(stake) click to toggle source

calculates the total return on a winning stake (the profit plus the stake) @param (see profit_on_stake) @return [Money] the total returned

# File lib/fixed_odds.rb, line 114
def total_return_on_stake stake
  profit_on_stake(stake) + stake
end

Protected Instance Methods

<=>(other) click to toggle source

low odds are those which pay out the most money on a winning bet and vice-versa

# File lib/fixed_odds.rb, line 162
def <=> other
  other.fractional_odds <=> @fractional_odds
end
==(other) click to toggle source

equality method

# File lib/fixed_odds.rb, line 156
def == other
  other.fractional_odds == @fractional_odds
end