All possible combinations of selected character substitution in a string in ruby
问题 I was wondering if there is a simple way to do every combination of selected character substitutions in ruby in a simple way. An example: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs.combination.each { |c| string.gsub c } would yield "this is @ test" "th!s !s a test" "thi$ i$ a te$t" "th!s !s @ test" "thi$ i$ @ te$t" "th!$ !$ a te$t" "th!$ !$ @ te$t" Thanks for the help! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1