pickup 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +33 -33
- data/lib/pickup.rb +4 -3
- data/lib/pickup/version.rb +1 -1
- data/spec/pickup/pickup_spec.rb +6 -5
- metadata +2 -2
data/README.md
CHANGED
@@ -20,52 +20,52 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
For example, we have got a pond with fish.
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
```ruby
|
24
|
+
pond = {
|
25
|
+
"selmon" => 1,
|
26
|
+
"carp" => 4,
|
27
|
+
"crucian" => 3,
|
28
|
+
"herring" => 6,
|
29
|
+
"sturgeon" => 8,
|
30
|
+
"gudgeon" => 10,
|
31
|
+
"minnow" => 20
|
32
|
+
}
|
33
|
+
```
|
34
34
|
Values are a chence to get this fish.
|
35
35
|
|
36
36
|
So we should create our pickup.
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
Look, we've just catched few minnows! To get selmon we need some
|
38
|
+
```ruby
|
39
|
+
pickup = Pickup.new(pond)
|
40
|
+
pickup.pick(3)
|
41
|
+
#=> [ "gudgeon", "minnow", "minnow" ]
|
42
|
+
```
|
43
|
+
Look, we've just catched few minnows! To get selmon we need some more tries ;)
|
44
44
|
|
45
45
|
Ok. What if our probability is not a linear function. We can create our pickup with a function:
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
```ruby
|
48
|
+
pickup = Pickup.new(pond){ |v| v**2 }
|
49
|
+
pickup.pick(3)
|
50
|
+
#=> ["carp", "selmon", "crucian"]
|
51
|
+
```
|
52
52
|
Wow, good catch!
|
53
53
|
|
54
54
|
Also you can change our `function` on the fly. Let's make square function:
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
56
|
+
```ruby
|
57
|
+
pickup = Pickup.new(pond)
|
58
|
+
pickup.pick_func = Proc.new{ |v| v**2 }
|
59
|
+
```
|
60
60
|
Or you can pass a block as a probability function wich will be applicable only to current operation
|
61
61
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
62
|
+
```ruby
|
63
|
+
pickup = Pickup.new(pond)
|
64
|
+
pickup.pick{ |v| Math.sin(v) } # same as pickup.pick(1){ ... }
|
65
|
+
#=> "selmon"
|
66
|
+
pickup.pick
|
67
|
+
#=> "minnow"
|
68
|
+
```
|
69
69
|
|
70
70
|
|
71
71
|
## Contributing
|
data/lib/pickup.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
require "pickup/version"
|
2
2
|
|
3
3
|
class Pickup
|
4
|
-
attr_reader :list
|
4
|
+
attr_reader :list, :uniq
|
5
5
|
attr_writer :pick_func
|
6
6
|
|
7
|
-
def initialize(list, &block)
|
7
|
+
def initialize(list, uniq=false, &block)
|
8
8
|
@list = list
|
9
|
+
@uniq = uniq
|
9
10
|
@pick_func = block if block_given?
|
10
11
|
end
|
11
12
|
|
12
13
|
def pick(count=1, &block)
|
13
14
|
func = block || pick_func
|
14
|
-
mlist = MappedList.new(list, func)
|
15
|
+
mlist = MappedList.new(list, func, uniq)
|
15
16
|
result = count.times.map do |i|
|
16
17
|
mlist.random
|
17
18
|
end
|
data/lib/pickup/version.rb
CHANGED
data/spec/pickup/pickup_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe Pickup do
|
|
16
16
|
@pickup = Pickup.new(@list)
|
17
17
|
end
|
18
18
|
|
19
|
-
it "should
|
19
|
+
it "should pick correct ammount of items" do
|
20
20
|
@pickup.pick(2).size.must_equal 2
|
21
21
|
end
|
22
22
|
|
@@ -26,23 +26,24 @@ describe Pickup do
|
|
26
26
|
@ml = Pickup::MappedList.new(@list, @func, true)
|
27
27
|
@ml2 = Pickup::MappedList.new(@list, @func)
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
|
+
it "should return right mapped list" do
|
30
31
|
@ml.list.must_equal(@mapped_list)
|
31
32
|
end
|
32
33
|
|
33
|
-
it "should return selmon and then carp and then crucian" do
|
34
|
+
it "should return selmon and then carp and then crucian for uniq pickup" do
|
34
35
|
@ml.get_random_item(1).must_equal "selmon"
|
35
36
|
@ml.get_random_item(1).must_equal "carp"
|
36
37
|
@ml.get_random_item(1).must_equal "crucian"
|
37
38
|
end
|
38
39
|
|
39
|
-
it "should return selmon 3 times" do
|
40
|
+
it "should return selmon 3 times for non-uniq pickup" do
|
40
41
|
@ml2.get_random_item(1).must_equal "selmon"
|
41
42
|
@ml2.get_random_item(1).must_equal "selmon"
|
42
43
|
@ml2.get_random_item(1).must_equal "selmon"
|
43
44
|
end
|
44
45
|
|
45
|
-
it "should return
|
46
|
+
it "should return item from the beginning after end of list for uniq pickup" do
|
46
47
|
@ml.get_random_item(30).must_equal "gudgeon"
|
47
48
|
@ml.get_random_item(30).must_equal "minnow"
|
48
49
|
@ml.get_random_item(30).must_equal "selmon"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pickup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
12
|
+
date: 2012-05-31 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Pickup helps you to pick item from collection by it's weight/probability
|
15
15
|
email:
|