I’d like to use two arrays to change characters in a string. The first array would have the original characters, the second would have the replacement characters.
original = ["a", "b", "c"]
replacements = ["x", "y", "z"]
text = "a xx b xx c"
# New string should be "x xx y xx z"
Is there an easy way to do this in Ruby?
You would use
String#trto do the replacements, andArray#jointo turn your arrays into strings, which is whatString#trexpects as arguments.rubyFiddle.