Class: Pickup
- Inherits:
-
Object
- Object
- Pickup
- Defined in:
- lib/pickup.rb,
lib/pickup/version.rb
Defined Under Namespace
Classes: CircleIterator, MappedList
Constant Summary collapse
- VERSION =
"0.0.11"
Instance Attribute Summary collapse
-
#key_func ⇒ Object
writeonly
Sets the attribute key_func.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
- #pick_func ⇒ Object
-
#uniq ⇒ Object
readonly
Returns the value of attribute uniq.
-
#weight_func ⇒ Object
writeonly
Sets the attribute weight_func.
Instance Method Summary collapse
-
#initialize(list, opts = {}, &block) ⇒ Pickup
constructor
A new instance of Pickup.
- #pick(count = 1, opts = {}, &block) ⇒ Object
Constructor Details
#initialize(list, opts = {}, &block) ⇒ Pickup
Returns a new instance of Pickup.
7 8 9 10 11 12 13 |
# File 'lib/pickup.rb', line 7 def initialize(list, opts={}, &block) @list = list @uniq = opts[:uniq] || false @pick_func = block if block_given? @key_func = opts[:key_func] @weight_func = opts[:weight_func] end |
Instance Attribute Details
#key_func=(value) ⇒ Object (writeonly)
Sets the attribute key_func
5 6 7 |
# File 'lib/pickup.rb', line 5 def key_func=(value) @key_func = value end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
4 5 6 |
# File 'lib/pickup.rb', line 4 def list @list end |
#pick_func ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/pickup.rb', line 24 def pick_func @pick_func ||= begin Proc.new do |val| val end end end |
#uniq ⇒ Object (readonly)
Returns the value of attribute uniq.
4 5 6 |
# File 'lib/pickup.rb', line 4 def uniq @uniq end |
#weight_func=(value) ⇒ Object (writeonly)
Sets the attribute weight_func
5 6 7 |
# File 'lib/pickup.rb', line 5 def weight_func=(value) @weight_func = value end |
Instance Method Details
#pick(count = 1, opts = {}, &block) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/pickup.rb', line 15 def pick(count=1, opts={}, &block) func = block || pick_func key_func = opts[:key_func] || @key_func weight_func = opts[:weight_func] || @weight_func mlist = MappedList.new(list, func, uniq: uniq, key_func: key_func, weight_func: weight_func) result = mlist.random(count) count == 1 ? result.first : result end |