module Enumerable
Public Instance Methods
bool_array_combs()
click to toggle source
Produces a array with values's all combinations.
Example:
%i[a b].boolean_combinations => [[], [:a], [:b], [:a, :b]]
@return [Array]
# File lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb, line 11 def bool_array_combs bool_combs([], method(:bool_array_combs_new_comb)) end
bool_hash_combs()
click to toggle source
Produces a hash with values's all combinations.
Example:
%i[a b].boolean_combinations => [{a: false, b: false}, {a: false, b: true}, {a: true, b: false}, {a: true, b: true}]
@return [Hash]
# File lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb, line 22 def bool_hash_combs bool_combs({}, method(:bool_hash_combs_new_comb)) end
Private Instance Methods
bool_array_combs_new_comb(value, combs)
click to toggle source
# File lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb, line 36 def bool_array_combs_new_comb(value, combs) combs + combs.map { |c| c + [value] } end
bool_combs(empty_value, new_comb_method)
click to toggle source
# File lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb, line 28 def bool_combs(empty_value, new_comb_method) head = [empty_value] r = inject(head) do |a, value| new_comb_method.call(value, a) end r == head ? [] : r end
bool_hash_combs_new_comb(value, combs)
click to toggle source
# File lib/eac_ruby_utils/patches/enumerable/boolean_combinations.rb, line 40 def bool_hash_combs_new_comb(value, combs) combs.flat_map do |comb| [false, true].map { |bool_value| comb.dup.merge(value => bool_value) } end end