class Fortune::P

Attributes

m[RW]
n[RW]
p[RW]

Public Class Methods

is(chance, n_all) click to toggle source
# File lib/fortune/p.rb, line 44
def self.is(chance, n_all)
  P.n(n_all) <= chance
end
n(n_all) click to toggle source
# 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 = {}) click to toggle source

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

|| P.n_select(10 => 90, 5 => 10)
|| P.n_select([:a, :b] => 1, [:c] => 2)
# File lib/fortune/p.rb, line 53
def self.n_select(h = {})
  P.n(Event.select(h))
end
new(m, n_all = nil) click to toggle source

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

# 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

Public Instance Methods

*(other) click to toggle source
# File lib/fortune/p.rb, line 19
def *(other)
  P.new(self.p * other.p)
end
+(other) click to toggle source
# File lib/fortune/p.rb, line 15
def +(other)
  P.new(self.p + other.p)
end
odds() click to toggle source
# File lib/fortune/p.rb, line 23
def odds
  Odds.new(:p => self.p)
end
to_human()
Alias for: to_percent_human
to_percent() click to toggle source
# File lib/fortune/p.rb, line 32
def to_percent
  self.p*100
end
to_percent_human() click to toggle source
# File lib/fortune/p.rb, line 27
def to_percent_human
  self.to_percent.round(2)
end
Also aliased as: to_human
to_percent_string() click to toggle source
# File lib/fortune/p.rb, line 36
def to_percent_string
  '%5.2f' % [self.to_percent] + '%'
end
value() click to toggle source
# File lib/fortune/p.rb, line 40
def value
  self.p
end