Class: Fortune::P
- Inherits:
-
Object
- Object
- Fortune::P
- Defined in:
- lib/fortune/p.rb
Instance Attribute Summary collapse
-
#m ⇒ Object
Returns the value of attribute m.
-
#n ⇒ Object
Returns the value of attribute n.
-
#p ⇒ Object
Returns the value of attribute p.
Class Method Summary collapse
- .is(chance, n_all) ⇒ Object
- .n(n_all) ⇒ Object
-
.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).
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
-
#initialize(m, n_all = nil) ⇒ P
constructor
P.new(:m => 1, :n => 10), P.new(1, 10), P.new(0.1).
- #odds ⇒ Object
- #to_percent ⇒ Object
- #to_percent_human ⇒ Object (also: #to_human)
- #to_percent_string ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(m, n_all = nil) ⇒ P
P.new(:m => 1, :n => 10), P.new(1, 10), P.new(0.1)
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
#m ⇒ Object
Returns the value of attribute m.
4 5 6 |
# File 'lib/fortune/p.rb', line 4 def m @m end |
#n ⇒ Object
Returns the value of attribute n.
4 5 6 |
# File 'lib/fortune/p.rb', line 4 def n @n end |
#p ⇒ Object
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 |
#to_percent ⇒ Object
32 33 34 |
# File 'lib/fortune/p.rb', line 32 def to_percent self.p*100 end |
#to_percent_human ⇒ Object 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_string ⇒ Object
36 37 38 |
# File 'lib/fortune/p.rb', line 36 def to_percent_string '%5.2f' % [self.to_percent] + '%' end |
#value ⇒ Object
40 41 42 |
# File 'lib/fortune/p.rb', line 40 def value self.p end |