lib/FixedOdds.rb in rodders-0.2.0 vs lib/FixedOdds.rb in rodders-0.3.0

- old
+ new

@@ -9,18 +9,20 @@ # the fractional odds as a Rational attr_reader :fractional_odds # 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] def FixedOdds.from_s(odds) case - when FixedOdds.fractional_odds?(odds) then FixedOdds.fractional_odds odds - when FixedOdds.moneyline_odds?(odds) then FixedOdds.moneyline_odds odds - when FixedOdds.decimal_odds?(odds) then FixedOdds.decimal_odds odds - else raise ArgumentError, %{could not parse "#{odds}"} + 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 # tells if the odds are in fractional form # @param [String] odds the odds representation @@ -53,11 +55,11 @@ # * evens # * even money # @param [String] fractional odds in fractional form # @return (see FixedOdds.from_s) def FixedOdds.fractional_odds(fractional) - raise %{could not parse "#{fractional}" as fractional odds} unless 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 @@ -69,14 +71,15 @@ end # 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) def FixedOdds.moneyline_odds(moneyline) - raise %{could not parse "#{moneyline}" as moneyline odds} unless 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 @@ -85,35 +88,35 @@ # * 1.25 # * 2 # @param [String] decimal odds in decimal form # @return (see FixedOdds.from_s) def FixedOdds.decimal_odds(decimal) - raise %{could not parse "#{decimal}" as decimal odds} unless FixedOdds.decimal_odds?(decimal) + raise %{could not parse "#{decimal}" as decimal odds} unless self.decimal_odds?(decimal) new(Rational(decimal.to_f - 1)) end # calculates the profit won on a winning bet - # @param [String] stake the stake + # @param [Money] stake the stake # @return [Money] the profit def profit_on_winning_stake(stake) - stake.to_money * @fractional_odds + stake * @fractional_odds end # calculates the total return on a winning bet # (which is the profit plus the initial stake) # @param (see #profit_on_winning_stake) # @return [Money] the total winnings def total_return_on_winning_stake(stake) - profit_on_winning_stake(stake) + stake.to_money + profit_on_winning_stake(stake) + stake end # calculates the magnitude of the stake needed to # win the specified amount in profit - # @param [String] win the desired profit + # @param [Money] win the desired profit # @return [Money] the stake required to realise that profit on a winning bet def stake_needed_to_win win - win.to_money / @fractional_odds + win / @fractional_odds end # string representation in fractional form like '4/1' # @return [String] fractional form representation def to_s @@ -127,11 +130,11 @@ end # string representation in moneyline form # @return [String] moneyline form representation def to_s_moneyline - integral_number_with_sign_regex = "%+d" + 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) @@ -139,19 +142,31 @@ end # string representation in decimal form # @return [String] decimal form representation def to_s_decimal - "%g" % (fractional_odds + 1) + '%g' % (fractional_odds + 1) end - # equality method - def ==(other) - other.fractional_odds == @fractional_odds - end + protected - # low odds are those which pay out the most money - # on a winning bet and vice-versa - def <=>(other) - other.fractional_odds <=> @fractional_odds - end + # equality method + def ==(other) + other.fractional_odds == @fractional_odds + end + + # low odds are those which pay out the most money + # on a winning bet and vice-versa + def <=>(other) + other.fractional_odds <=> @fractional_odds + end end + +class Rational + # calculates the reciprocal + # @example + # Rational(2/3).reciprocal #=> Rational(3/2) + # @return [Rational] the reciprocal + def reciprocal + 1 / self + end +end \ No newline at end of file