lib/regexp-examples/helpers.rb in regexp-examples-0.1.0 vs lib/regexp-examples/helpers.rb in regexp-examples-0.2.0
- old
+ new
@@ -9,10 +9,28 @@
# 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|
- options[:no_join] ? result.flatten : result.flatten.join
+ 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
# TODO: For debugging only!! Delete this before v1.0
def self.show(regexp)