chance 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/{README → README.markdown} +10 -9
- data/examples/dice.rb +0 -2
- data/examples/schroedinger.rb +0 -2
- data/examples/weather.rb +0 -2
- data/lib/chance.rb +1 -1
- data/lib/chance/core_extensions.rb +12 -10
- data/lib/chance/version.rb +2 -2
- data/spec/lib/core_extensions_spec.rb +15 -1
- metadata +4 -5
- data/lib/chance/chance_case.rb +0 -19
data/{README → README.markdown}
RENAMED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
Chance
|
2
|
+
=========
|
2
3
|
|
3
4
|
Chance is a little Ruby library for expressing uncertainty in your code. Maybe you always wanted to program with probability?
|
4
5
|
|
@@ -8,20 +9,20 @@ Date#at_some_point rather than Date#midnight
|
|
8
9
|
Array#pick(percentage) rather than iterating over every element
|
9
10
|
|
10
11
|
"maybe" is a Kernel method that randomly evaluates to true or false when it is called.
|
11
|
-
|
12
|
+
|
13
|
+
`@bob.lucky_winner? = maybe`
|
12
14
|
# => true
|
13
|
-
|
15
|
+
`@chauncey.lucky_winner? = maybe`
|
14
16
|
# => false
|
15
17
|
|
16
18
|
When supplied with a block, it will call it. Or not. Half of the time it just returns nil. For example
|
17
19
|
|
18
|
-
maybe {rotate_logs}
|
20
|
+
`maybe {rotate_logs}`
|
19
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
23
|
|
22
|
-
30.percent.chance.of { "rain" }
|
23
|
-
|
24
|
-
== Running examples ==
|
25
|
-
If you have the bundler gem installed, just run
|
24
|
+
`30.percent.chance.of { "rain" }`
|
26
25
|
|
27
|
-
|
26
|
+
Running examples
|
27
|
+
----------------
|
28
|
+
Make sure you have Bundler installed- then run `bundle exec rake`.
|
data/examples/dice.rb
CHANGED
data/examples/schroedinger.rb
CHANGED
data/examples/weather.rb
CHANGED
data/lib/chance.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
chance_lib_dir = File.expand_path(File.dirname(__FILE__)) + "/chance/"
|
2
|
-
['core_extensions', 'percentage', 'chance'
|
2
|
+
['core_extensions', 'percentage', 'chance'].each {|filename| require chance_lib_dir + filename}
|
@@ -34,13 +34,6 @@ class String
|
|
34
34
|
Percentage.new((second / first) * 100)
|
35
35
|
end
|
36
36
|
end
|
37
|
-
#
|
38
|
-
# class Date
|
39
|
-
# def at_some_point
|
40
|
-
# (at_midnight..tomorrow.at_midnight).to_a.rand
|
41
|
-
# end
|
42
|
-
# alias :whenever :at_some_point
|
43
|
-
# end
|
44
37
|
|
45
38
|
class Array
|
46
39
|
def random
|
@@ -57,9 +50,18 @@ class Array
|
|
57
50
|
end
|
58
51
|
|
59
52
|
def pick_about(percentage)
|
60
|
-
select
|
61
|
-
percentage.chance.happens?
|
62
|
-
end
|
53
|
+
select {|element| percentage.chance.happens? }
|
63
54
|
end
|
64
55
|
|
65
56
|
end
|
57
|
+
|
58
|
+
class Range
|
59
|
+
def random
|
60
|
+
step_to, i = rand(count), 0
|
61
|
+
|
62
|
+
step do |obj|
|
63
|
+
return obj if i == step_to
|
64
|
+
i += 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
data/lib/chance/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.5.
|
1
|
+
class Chance
|
2
|
+
VERSION = "0.5.1"
|
3
3
|
end
|
@@ -62,6 +62,20 @@ describe "Extensions to core classes;" do
|
|
62
62
|
a.should include(a.random)
|
63
63
|
end
|
64
64
|
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "Range" do
|
68
|
+
describe "#random" do
|
69
|
+
it "steps through the range and returns a random element from a numeric range" do
|
70
|
+
numeric_range = 1..10
|
71
|
+
numeric_range.should include numeric_range.random
|
72
|
+
end
|
65
73
|
|
74
|
+
it "steps through the range and returns a random element from a range of strings" do
|
75
|
+
string_range = 'a'..'z'
|
76
|
+
string_range.should include string_range.random
|
77
|
+
end
|
78
|
+
end
|
66
79
|
end
|
67
|
-
|
80
|
+
|
81
|
+
end
|
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
|
+
- 1
|
9
|
+
version: 0.5.1
|
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-14 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -31,7 +31,7 @@ files:
|
|
31
31
|
- .gitignore
|
32
32
|
- COPYING
|
33
33
|
- Gemfile
|
34
|
-
- README
|
34
|
+
- README.markdown
|
35
35
|
- Rakefile
|
36
36
|
- chance.gemspec
|
37
37
|
- examples/dice.rb
|
@@ -39,7 +39,6 @@ files:
|
|
39
39
|
- examples/weather.rb
|
40
40
|
- lib/chance.rb
|
41
41
|
- lib/chance/chance.rb
|
42
|
-
- lib/chance/chance_case.rb
|
43
42
|
- lib/chance/core_extensions.rb
|
44
43
|
- lib/chance/percentage.rb
|
45
44
|
- lib/chance/version.rb
|
data/lib/chance/chance_case.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
class ChanceCase
|
2
|
-
attr_reader :outcome
|
3
|
-
|
4
|
-
def self.test(&block)
|
5
|
-
puts 'Here goes:'
|
6
|
-
case block.arity
|
7
|
-
when 0
|
8
|
-
yield
|
9
|
-
when 1
|
10
|
-
yield 'one'
|
11
|
-
when 2
|
12
|
-
yield 'one', 'two'
|
13
|
-
when 3
|
14
|
-
yield 'one', 'two', 'three'
|
15
|
-
end
|
16
|
-
puts 'Done!'
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|