Class: Fortune::Odds
Overview
win
Instance Attribute Summary collapse
-
#k ⇒ Object
Returns the value of attribute k.
-
#p ⇒ Object
Returns the value of attribute p.
-
#s ⇒ Object
Returns the value of attribute s.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(h = {}) ⇒ Odds
constructor
A new instance of Odds.
- #is_on_lose? ⇒ Boolean
- #is_on_win? ⇒ Boolean
- #n_all ⇒ Object (also: #variants)
- #p_obj ⇒ Object
- #revert ⇒ Object
- #s_to_k ⇒ Object
-
#to_human(h = {:k => 7}) ⇒ Object
h = => int, [:fractions => true].
- #to_i ⇒ Object
- #to_lose ⇒ Object
- #to_s ⇒ Object
- #to_win ⇒ Object
Constructor Details
#initialize(h = {}) ⇒ Odds
Returns a new instance of Odds.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fortune/odds.rb', line 8 def initialize(h = {}) # TODO: check p or (s and k) exists h[:s] ||= h[:win] h[:k] ||= h[:lose] [:win, :lose].each{|k| h.delete(k)} h.each{|k,v| instance_variable_set("@#{k}", v)} self.p = P.new(s, s + k).value unless self.p self.calc_s_k if !self.k || !self.s self.to_win end |
Instance Attribute Details
#k ⇒ Object
Returns the value of attribute k.
6 7 8 |
# File 'lib/fortune/odds.rb', line 6 def k @k end |
#p ⇒ Object
Returns the value of attribute p.
6 7 8 |
# File 'lib/fortune/odds.rb', line 6 def p @p end |
#s ⇒ Object
Returns the value of attribute s.
6 7 8 |
# File 'lib/fortune/odds.rb', line 6 def s @s end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/fortune/odds.rb', line 6 def type @type end |
Class Method Details
.equal ⇒ Object
87 88 89 |
# File 'lib/fortune/odds.rb', line 87 def self.equal Odds.new({:s => 1, :k => 1}) end |
.human(h) ⇒ Object
91 92 93 |
# File 'lib/fortune/odds.rb', line 91 def self.human(h) Odds.p_human(h).values end |
Instance Method Details
#<=>(other) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/fortune/odds.rb', line 21 def <=>(other) if self.p < other.p -1 elsif self.p > other.p 1 else 0 end end |
#is_on_lose? ⇒ Boolean
61 62 63 |
# File 'lib/fortune/odds.rb', line 61 def is_on_lose? self.type == :on_lose end |
#is_on_win? ⇒ Boolean
57 58 59 |
# File 'lib/fortune/odds.rb', line 57 def is_on_win? self.type == :on_win end |
#n_all ⇒ Object Also known as: variants
36 37 38 |
# File 'lib/fortune/odds.rb', line 36 def n_all s + k end |
#revert ⇒ Object
81 82 83 84 85 |
# File 'lib/fortune/odds.rb', line 81 def revert self.s_to_k self.p = 1 - self.p self end |
#s_to_k ⇒ Object
71 72 73 |
# File 'lib/fortune/odds.rb', line 71 def s_to_k self.s, self.k = self.k, self.s end |
#to_human(h = {:k => 7}) ⇒ Object
h = => int, [:fractions => true]
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fortune/odds.rb', line 42 def to_human(h = {:k => 7}) min_delta = nil closest_odd = nil Odds.human(h).each{|odd| delta = (self.p - odd.p).abs if !min_delta || delta < min_delta min_delta = delta closest_odd = odd end } closest_odd end |
#to_i ⇒ Object
31 32 33 34 |
# File 'lib/fortune/odds.rb', line 31 def to_i self.s, self.k = self.s.to_i, self.k.to_i self end |
#to_lose ⇒ Object
75 76 77 78 79 |
# File 'lib/fortune/odds.rb', line 75 def to_lose self.s_to_k if self.is_on_win? self.type = :on_lose self end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/fortune/odds.rb', line 95 def to_s "#{self.s}:#{self.k} #{self.type} (p: #{p_obj.to_percent_string})" end |
#to_win ⇒ Object
65 66 67 68 69 |
# File 'lib/fortune/odds.rb', line 65 def to_win self.s_to_k if self.is_on_lose? self.type = :on_win self end |