chance 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- `@bob.lucky_winner? = maybe`
14
- # => true
15
- `@chauncey.lucky_winner? = maybe`
16
- # => false
13
+ bob.lucky_winner? = maybe
14
+ # => true
17
15
 
18
- When supplied with a block, it will call it. Or not. Half of the time it just returns nil. For example
16
+ chauncey.lucky_winner? = maybe
17
+ # => false
19
18
 
20
- `maybe {rotate_logs}`
19
+ Maybe can also be supplied with a block, which will be called only if the Chance happens:
21
20
 
22
- By default, maybe is 50/50. You can also use "probably", "rarely" and "almost_never", or just create your own Chance object like so:
21
+ maybe do
22
+ rotate_logs
23
+ end
23
24
 
24
- `30.percent.chance.of { "rain" }`
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
- Running examples
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`.
@@ -40,6 +40,10 @@ class Array
40
40
  self[rand(length)]
41
41
  end
42
42
 
43
+ def random_pop
44
+ delete_at rand(length)
45
+ end
46
+
43
47
  def pick(percent)
44
48
  picks, percentage = [], percent.of(length).round
45
49
  while picks.length < percentage
@@ -1,3 +1,3 @@
1
1
  class Chance
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -5,4 +5,5 @@ describe Die do
5
5
  @d6 = Die.new(6)
6
6
  (1..6).should include(@d6.roll)
7
7
  end
8
- end
8
+ end
9
+
@@ -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(a.random)
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
- - 1
9
- version: 0.5.1
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-14 00:00:00 -04:00
17
+ date: 2011-07-19 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies: []
20
20