I have a method that I want to use to replace characters in a string:
def complexity_level_two
replacements = {
'i' => 'eye', 'e' => 'eei',
'a' => 'aya', 'o' => 'oha'}
word = "Cocoa!55"
word_arr = word.split('')
results = []
word_arr.each { |char|
if replacements[char] != nil
results.push(char.to_s.gsub!(replacements[char]))
else
results.push(char)
end
}
end
My desired output for the string should be: Cohacohaaya!55
However when I run this method, it will not replace the characters and only outputs the string:
C
o
c
o
a
!
5
5
What am I doing wrong that makes this method not replace the correct characters inside of the string to match that in the hash
? How can I fix this to get the desired output?
Regexp::union
,String#gsub
with hash.