Class: Odds
Instance Attribute Summary collapse
-
#win_chance ⇒ Object
Returns the value of attribute win_chance.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
-
#initialize(arg) ⇒ Odds
constructor
A new instance of Odds.
- #inverse ⇒ Object
- #log_odds ⇒ Object
- #loss_chance ⇒ Object
- #odds ⇒ Object
- #to_s ⇒ Object
- #to_s_to_one ⇒ Object
- #win_amount_with_principal ⇒ Object
- #win_amount_without_principal ⇒ Object
Constructor Details
#initialize(arg) ⇒ Odds
Returns a new instance of Odds.
5 6 7 8 9 10 11 |
# File 'lib/odds/odds.rb', line 5 def initialize(arg) if arg.kind_of?(Hash) from_hash(arg) else self.win_chance = arg end end |
Instance Attribute Details
#win_chance ⇒ Object
Returns the value of attribute win_chance.
3 4 5 |
# File 'lib/odds/odds.rb', line 3 def win_chance @win_chance end |
Class Method Details
.from_string(str) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/odds/odds.rb', line 61 def from_string(str) sign = str[0..0] num = str[1..-1].to_f / 100.0 if sign == '-' from_win_amount_without_principal(1.0 / num) elsif sign == '+' from_win_amount_without_principal(num) else raise "bad #{str}" end end |
.from_win_amount_without_principal(num) ⇒ Object
72 73 74 |
# File 'lib/odds/odds.rb', line 72 def from_win_amount_without_principal(num) new(:win_chance => (1.0 / (1.0 + num))) end |
Instance Method Details
#*(other) ⇒ Object
43 44 45 46 |
# File 'lib/odds/odds.rb', line 43 def* (other) res = win_chance * other.win_chance self.class.new(:win_chance => res) end |
#inverse ⇒ Object
48 49 50 |
# File 'lib/odds/odds.rb', line 48 def inverse self.class.new(:win_chance => (1.0 - win_chance)) end |
#log_odds ⇒ Object
56 57 58 |
# File 'lib/odds/odds.rb', line 56 def log_odds Math.log(odds) end |
#loss_chance ⇒ Object
37 38 39 |
# File 'lib/odds/odds.rb', line 37 def loss_chance 1.0 - win_chance end |
#odds ⇒ Object
52 53 54 |
# File 'lib/odds/odds.rb', line 52 def odds win_chance / (1.0 - win_chance) end |
#to_s ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/odds/odds.rb', line 13 def to_s if win_chance < 0.5 "+" + (win_amount_without_principal * 100.0).to_i.to_s else n = (1.0 / loss_chance) - 1.0 "-" + (n * 100.0).to_i.to_s end end |
#to_s_to_one ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/odds/odds.rb', line 22 def to_s_to_one if win_chance < 0.5 "#{win_amount_without_principal} to 1" else n = (1.0 / win_amount_without_principal).round(1) "1 to #{n}" end end |
#win_amount_with_principal ⇒ Object
31 32 33 |
# File 'lib/odds/odds.rb', line 31 def win_amount_with_principal 1.0 / win_chance end |
#win_amount_without_principal ⇒ Object
34 35 36 |
# File 'lib/odds/odds.rb', line 34 def win_amount_without_principal win_amount_with_principal - 1.0 end |