pickup 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
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
- ```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
- ```
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
- ```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 mor tries ;)
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
- ```ruby
48
- pickup = Pickup.new(pond){ |v| v**2 }
49
- pickup.pick(3)
50
- #=> ["carp", "selmon", "crucian"]
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
- ```ruby
57
- pickup = Pickup.new(pond)
58
- pickup.pick_func = Proc.new{ |v| v**2 }
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
- ```ruby
63
- pickup = Pickup.new(pond)
64
- pickup.pick{ |v| 1/v } # same as pickup.pick(1){ ... }
65
- #=> "selmon"
66
- pickup.pick
67
- #=> "minnow"
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
@@ -1,3 +1,3 @@
1
1
  class Pickup
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -16,7 +16,7 @@ describe Pickup do
16
16
  @pickup = Pickup.new(@list)
17
17
  end
18
18
 
19
- it "should be equal" do
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
- it "should return mapped list" do
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 first item after last" do
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.1
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-29 00:00:00.000000000 Z
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: