Module: Combos
- Defined in:
- lib/combos.rb,
lib/combos/version.rb,
lib/combos/match_maker.rb
Constant Summary
- VERSION =
"0.1.0"- SPEC_SUMMARY =
<<EOF A helpful gem that can be used to generate combinations on the fly~ EOF
- SPEC_DESCRIPTION =
<<EOF Provides methods in the Combos module that you can use for: power_pair serial_pair random_pair combo_pair EOF
- SPEC_HOMEPAGE =
<<EOF https://ufo2mstar.github.io/combos/docs/ EOF
- SPEC_METADATA =
<<EOF http://mygemserver.com'" EOF
Instance Method Summary collapse
-
#brute_pair(*true_params, &blk) ⇒ Array(Arrays(Objects))
private
The Array of combinations.
-
#combo_pair(num, *params, &blk) ⇒ Object
Generates Combos to hit All the variables atleast once.
-
#power_pair(*params, &blk) ⇒ Object
Generates All possible Combos of all your array elements.
- #random_pair(*params, &blk) ⇒ Object
-
#serial_pair(*params, &blk) ⇒ Object
Generates Combos to hit All the variables atleast once.
-
#smart_pair(rnd = nil, *true_params, &blk) ⇒ Array(Arrays(Objects))
private
The Array of combinations.
-
#vprint(*args) ⇒ Object
protected
print if $VERBOSE flag is truthy.
-
#vputs(*args) ⇒ Object
protected
puts if $VERBOSE flag is truthy.
Instance Method Details
#brute_pair(*true_params, &blk) ⇒ Array(Arrays(Objects)) (private)
Returns the Array of combinations
57 58 59 60 61 62 63 |
# File 'lib/combos/match_maker.rb', line 57 def brute_pair *true_params, &blk params = true_params.map(&:dup) n =params.length params.each_with_index { |a, i| eval "@a#{i} = #{a}" } @combos = n!=1 ? eval("#{k = '@a0'; (n-1).times { |i| k+=".product(@a#{i+1})" }; k+=".map(&:flatten)"}") : @a0 #.map{|x|[x]} blk ? @combos.each { |a| yield a } : @combos end |
#combo_pair(num, *params, &blk) ⇒ Object
Generates Combos to hit All the variables atleast once
30 31 32 |
# File 'lib/combos.rb', line 30 def combo_pair num, *params, &blk smart_pair num, *params, &blk end |
#power_pair(*params, &blk) ⇒ Object
Generates All possible Combos of all your array elements
10 11 12 |
# File 'lib/combos.rb', line 10 def power_pair *params, &blk brute_pair *params, &blk end |
#random_pair(*params, &blk) ⇒ Object
25 26 27 |
# File 'lib/combos.rb', line 25 def random_pair *params, &blk smart_pair "rand", *params, &blk end |
#serial_pair(*params, &blk) ⇒ Object
Generates Combos to hit All the variables atleast once
18 19 20 |
# File 'lib/combos.rb', line 18 def serial_pair *params, &blk smart_pair nil, *params, &blk end |
#smart_pair(rnd = nil, *true_params, &blk) ⇒ Array(Arrays(Objects)) (private)
Returns the Array of combinations
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/combos/match_maker.rb', line 22 def smart_pair rnd = nil, *true_params, &blk safe_quit=-> { vputs "exiting smart_pair"; [] } begin params = true_params.map(&:dup) rescue TypeError => e vputs "#{e.class}: #{e.}" return safe_quit[] rescue Exception => e vputs "#{e.class}: #{e.}" return safe_quit[] end params = [] if params.nil? (vprint "params are empty.."; return safe_quit[]) if params.flatten.empty? # if rnd is a number, increase the first ary with rand entries (assuming first ary entry is larger) if rnd.class == Fixnum src_ary = params.first diff = rnd - src_ary.size res = diff<1 ? (diff = rnd; []) : src_ary # assign blank and make the rnd sampling == rnd res += diff.times.map { | | src_ary.sample } params[0] = res end n =params.length (vputs "No Ary passed for smart_pairing", :rnd; return) if n == 0 params.each_with_index { |a, i| eval "@a#{i} = #{a}" } lim = params.max_by(&:length).size rand_samp =-> ary { ary.shuffle! if rnd; ary.shift } @combos = lim.times.map { params.each_with_index.map { |ary, i| eval "ary=[@a#{i}.sample]" if ary.empty?; rand_samp[ary] } } blk ? @combos.each { |a| yield a } : @combos end |
#vprint(*args) ⇒ Object (protected)
print if $VERBOSE flag is truthy
10 11 12 |
# File 'lib/combos/match_maker.rb', line 10 def vprint *args print *args if $VERBOSE end |
#vputs(*args) ⇒ Object (protected)
puts if $VERBOSE flag is truthy
5 6 7 |
# File 'lib/combos/match_maker.rb', line 5 def vputs *args puts *args if $VERBOSE end |