Class: Fortune::Odds

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/fortune/odds.rb

Overview

win

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#kObject

Returns the value of attribute k.



6
7
8
# File 'lib/fortune/odds.rb', line 6

def k
  @k
end

#pObject

Returns the value of attribute p.



6
7
8
# File 'lib/fortune/odds.rb', line 6

def p
  @p
end

#sObject

Returns the value of attribute s.



6
7
8
# File 'lib/fortune/odds.rb', line 6

def s
  @s
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/fortune/odds.rb', line 6

def type
  @type
end

Class Method Details

.equalObject



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

Returns:

  • (Boolean)


61
62
63
# File 'lib/fortune/odds.rb', line 61

def is_on_lose?
  self.type == :on_lose
end

#is_on_win?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fortune/odds.rb', line 57

def is_on_win?
  self.type == :on_win
end

#n_allObject Also known as: variants



36
37
38
# File 'lib/fortune/odds.rb', line 36

def n_all
  s + k
end

#p_objObject



99
100
101
# File 'lib/fortune/odds.rb', line 99

def p_obj
  P.new(self.p)
end

#revertObject



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_kObject



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_iObject



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_loseObject



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_sObject



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_winObject



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