Class: Fortune::P

Inherits:
Object
  • Object
show all
Defined in:
lib/fortune/p.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, n_all = nil) ⇒ P

P.new(:m => 1, :n => 10), P.new(1, 10), P.new(0.1)

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
# File 'lib/fortune/p.rb', line 7

def initialize(m, n_all = nil)
  m, n_all = m[:m], m[:n] if m.is_a?(Hash)
  raise ArgumentError.new("Error: p should be less or equal 1") if (!n_all && m > 1) || (n_all && m > n_all)
  @p = m unless n_all
  @p = m.to_f/n_all.to_f if n_all
  @m, @n = m, n_all if n_all
end

Instance Attribute Details

#mObject

Returns the value of attribute m.



4
5
6
# File 'lib/fortune/p.rb', line 4

def m
  @m
end

#nObject

Returns the value of attribute n.



4
5
6
# File 'lib/fortune/p.rb', line 4

def n
  @n
end

#pObject

Returns the value of attribute p.



4
5
6
# File 'lib/fortune/p.rb', line 4

def p
  @p
end

Class Method Details

.is(chance, n_all) ⇒ Object



44
45
46
# File 'lib/fortune/p.rb', line 44

def self.is(chance, n_all)
  P.n(n_all) <= chance
end

.n(n_all) ⇒ Object



47
48
49
# File 'lib/fortune/p.rb', line 47

def self.n(n_all)
  n_all.is_a?(Array) ? n_all.sample : rand(n_all) + 1
end

.n_select(h = {}) ⇒ Object

P.n_select( => 5, [3..15] => 20, …)

|| P.n_select(10 => 90, 5 => 10)
|| P.n_select([:a, :b] => 1, [:c] => 2)


53
54
55
# File 'lib/fortune/p.rb', line 53

def self.n_select(h = {})
  P.n(Event.select(h))
end

Instance Method Details

#*(other) ⇒ Object



19
20
21
# File 'lib/fortune/p.rb', line 19

def *(other)
  P.new(self.p * other.p)
end

#+(other) ⇒ Object



15
16
17
# File 'lib/fortune/p.rb', line 15

def +(other)
  P.new(self.p + other.p)
end

#oddsObject



23
24
25
# File 'lib/fortune/p.rb', line 23

def odds
  Odds.new(:p => self.p)
end

#to_percentObject



32
33
34
# File 'lib/fortune/p.rb', line 32

def to_percent
  self.p*100
end

#to_percent_humanObject Also known as: to_human



27
28
29
# File 'lib/fortune/p.rb', line 27

def to_percent_human
  self.to_percent.round(2)
end

#to_percent_stringObject



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

def to_percent_string
  '%5.2f' % [self.to_percent] + '%'
end

#valueObject



40
41
42
# File 'lib/fortune/p.rb', line 40

def value
  self.p
end