chance 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +25 -10
- data/lib/chance/core_extensions.rb +4 -0
- data/lib/chance/version.rb +1 -1
- data/spec/examples/dice_spec.rb +2 -1
- data/spec/lib/core_extensions_spec.rb +13 -1
- metadata +3 -3
data/README.markdown
CHANGED
@@ -10,19 +10,34 @@ Array#pick(percentage) rather than iterating over every element
|
|
10
10
|
|
11
11
|
"maybe" is a Kernel method that randomly evaluates to true or false when it is called.
|
12
12
|
|
13
|
-
|
14
|
-
# => true
|
15
|
-
`@chauncey.lucky_winner? = maybe`
|
16
|
-
# => false
|
13
|
+
bob.lucky_winner? = maybe
|
14
|
+
# => true
|
17
15
|
|
18
|
-
|
16
|
+
chauncey.lucky_winner? = maybe
|
17
|
+
# => false
|
19
18
|
|
20
|
-
|
19
|
+
Maybe can also be supplied with a block, which will be called only if the Chance happens:
|
21
20
|
|
22
|
-
|
21
|
+
maybe do
|
22
|
+
rotate_logs
|
23
|
+
end
|
23
24
|
|
24
|
-
`
|
25
|
+
Behind the scenes, `maybe` is just constructing a Chance object. It's equivalent to the statement `50.percent.chance.of {rotate_logs}`
|
25
26
|
|
26
|
-
|
27
|
+
By themselves, Chance objects either "happen" or they don't- the probability for each is evaluated the first time you call `Chance.happen?`, and thereafter it's set in stone. See the schroedinger.rb example if this interests you.
|
28
|
+
|
29
|
+
Chance Case Statements
|
30
|
+
--------------------
|
31
|
+
|
32
|
+
Chance Cases take any number of args, each one being a probability statement with an outcome block attached. The probabilities must add to 100 percent (sorry, for once in your life you will have to give less than 110%). Only one outcome will be evaluated, as you would expect:
|
33
|
+
|
34
|
+
outcome = Chance.case(
|
35
|
+
70.percent.chance.will {'snow'},
|
36
|
+
20.percent.chance.will {'sleet'},
|
37
|
+
8.percent.chance.will {'sun'},
|
38
|
+
2.percent.chance.will {'knives'}
|
39
|
+
)
|
40
|
+
|
41
|
+
Running examples and specs
|
27
42
|
----------------
|
28
|
-
Make sure you have Bundler installed- then run `bundle exec rake`.
|
43
|
+
Check out the specs for a better idea of how to use Chance. Make sure you have Bundler installed- then run `bundle exec rake`.
|
data/lib/chance/version.rb
CHANGED
data/spec/examples/dice_spec.rb
CHANGED
@@ -59,9 +59,21 @@ describe "Extensions to core classes;" do
|
|
59
59
|
describe "#random" do
|
60
60
|
it "returns a randomly selected element" do
|
61
61
|
a = [1,2,3]
|
62
|
-
a.should include
|
62
|
+
a.should include a.random
|
63
63
|
end
|
64
64
|
end
|
65
|
+
|
66
|
+
describe "#random_pop" do
|
67
|
+
it "removes a randomly selected element and returns it" do
|
68
|
+
a = [1,2,3]
|
69
|
+
a.size.should be 3
|
70
|
+
|
71
|
+
popped = a.random_pop
|
72
|
+
a.size.should be 2
|
73
|
+
a.should_not include popped
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
65
77
|
end
|
66
78
|
|
67
79
|
context "Range" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 2
|
9
|
+
version: 0.5.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Patrick Ewing
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-19 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|