Topic: How to get combination of words with replaced letter

I have a short question (hopefully). I need the method (perhaps it already exists...?) that I describe below.

Situation:

I have a word: "abcxdex".
If the word contains the character "x", return all combinations of possible words with "x" replaced by "y".

The result should therefore be an array of such words as:

"abcxdex"
"abcydex"
"abcydey"
"abcxdey"

Has someone an idea?

Last edited by emmek (2011-01-13 06:58:53)

Re: How to get combination of words with replaced letter

I don't think there is a ready-to-use Ruby method to get what you want smile. You will have to play with regular epressions and implement your own algo to achieve it.

Here is just an idea:

word = "abcxdex"
pattern = /x/
while pattern.match(word)
  word.sub!('x','y')
  puts "replaced word: #{word}"
end

It's up to you to adapt it for your needs.

Last edited by Javix (2011-01-13 08:08:06)