Sha256: e83c873c1e696827dc472b1ae6797a4972024b111007213e9b9d0a03d3f7ba1e
Contents?: true
Size: 1.03 KB
Versions: 1
Compression:
Stored size: 1.03 KB
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| if options[:no_join] result.flatten else join_preserving_capture_groups(result) end end end def self.join_preserving_capture_groups(result) result.flatten! subgroups = result .select { |partial| partial.respond_to? :group_id } .map(&:all_subgroups) .flatten if subgroups.empty? result.join else CaptureGroupResult.new(nil, subgroups, result.join) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
regexp-examples-0.2.1 | lib/regexp-examples/helpers.rb |