Sha256: a973d49db3146e269f8840c21ab6370966e6e9dd381438c5af9ac81c07c17b35
Contents?: true
Size: 855 Bytes
Versions: 5
Compression:
Stored size: 855 Bytes
Contents
module RegexpExamples # Given an array of arrays of strings, # returns all possible perutations, # for strings created by joining one # element from each array # # For example: # permutations_of_strings [ ['a'], ['b'], ['c', 'd', 'e'] ] #=> ['acb', 'abd', 'abe'] # permutations_of_strings [ ['a', 'b'], ['c', 'd'] ] #=> [ 'ac', 'ad', 'bc', 'bd' ] def self.permutations_of_strings(arrays_of_strings, options={}) first = arrays_of_strings.shift return first if arrays_of_strings.empty? first.product( permutations_of_strings(arrays_of_strings, options) ).map do |result| join_preserving_capture_groups(result) end end def self.join_preserving_capture_groups(result) result.flatten! subgroups = result .map(&:all_subgroups) .flatten GroupResult.new(result.join, nil, subgroups) end end
Version data entries
5 entries across 5 versions & 1 rubygems