Class: FixedOdds
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
-
#fractional_odds ⇒ Object
readonly
the fractional odds as a Rational.
Class Method Summary collapse
-
.decimal_odds(decimal) ⇒ FixedOdds
creates a new FixedOdds from decimal form.
-
.decimal_odds?(odds) ⇒ Boolean
tells if the odds are in decimal form.
-
.fractional_odds(fractional) ⇒ FixedOdds
creates a new FixedOdds from fractional form.
-
.fractional_odds?(odds) ⇒ Boolean
tells if the odds are in fractional form.
-
.from_s(odds) ⇒ FixedOdds
creates a new FixedOdds from a string which can be in fractional, moneyline or decimal format equivalent to ‘5/1’.
-
.moneyline_odds(moneyline) ⇒ FixedOdds
creates a new FixedOdds from moneyline form.
-
.moneyline_odds?(odds) ⇒ Boolean
tells if the odds are in moneyline form.
Instance Method Summary collapse
-
#initialize(fractional_odds) ⇒ FixedOdds
constructor
creates a new FixedOdds from a Rational.
-
#profit_on_winning_stake(stake) ⇒ Money
calculates the profit won on a winning bet.
-
#stake_needed_to_win(win) ⇒ Money
calculates the magnitude of the stake needed to win the specified amount in profit.
-
#to_s ⇒ String
string representation in fractional form like ‘4/1’.
-
#to_s_decimal ⇒ String
string representation in decimal form.
-
#to_s_fractional ⇒ String
string representation in fractional form like ‘4/1’.
-
#to_s_moneyline ⇒ String
string representation in moneyline form.
-
#total_return_on_winning_stake(stake) ⇒ Money
calculates the total return on a winning bet (which is the profit plus the initial stake).
Constructor Details
#initialize(fractional_odds) ⇒ FixedOdds
creates a new FixedOdds from a Rational
69 70 71 |
# File 'lib/FixedOdds.rb', line 69 def initialize(fractional_odds) @fractional_odds = fractional_odds end |
Instance Attribute Details
#fractional_odds ⇒ Object (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
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
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
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
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
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’
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
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
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
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
116 117 118 |
# File 'lib/FixedOdds.rb', line 116 def stake_needed_to_win win win / @fractional_odds end |
#to_s ⇒ String
string representation in fractional form like ‘4/1’
122 123 124 |
# File 'lib/FixedOdds.rb', line 122 def to_s to_s_fractional end |
#to_s_decimal ⇒ String
string representation in decimal form
146 147 148 |
# File 'lib/FixedOdds.rb', line 146 def to_s_decimal '%g' % (fractional_odds + 1) end |
#to_s_fractional ⇒ String
string representation in fractional form like ‘4/1’
128 129 130 |
# File 'lib/FixedOdds.rb', line 128 def to_s_fractional @fractional_odds.to_s end |
#to_s_moneyline ⇒ String
string representation in moneyline form
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)
108 109 110 |
# File 'lib/FixedOdds.rb', line 108 def total_return_on_winning_stake(stake) profit_on_winning_stake(stake) + stake end |