Question
I saw this same question at Replace string with values from two arrays and thought I'd ask and respond to it in Ruby code (as the question was in Javascript)
I have a string for example:
str = 'This is a text that needs to change'
and two arrays:
arr0 = %w[a e i o u]
arr1 = %w[1 2 3 4 5]
I want to replace characters in str and arr0 with their corresponding arr1 values. The output should be:
'Th3s 3s 1 t2xt th1t n22ds to ch1ng2'
Since I plan to use this with large chunks of data, I expect the solution to be efficient.
Solution
str.gsub(Regexp.new(/[#{arr0.join('')}]/), Hash[[arr0, arr1].transpose])
=> "Th3s 3s 1 t2xt th1t n22ds t4 ch1ng2"
Feel free to share your solution to this problem =)