Refactor multiple gsub statements into 1

gsub, ruby

Solution

The best way is

str.tr('aeiou', 'AEIOU')

`String#tr`

Returns a copy of `str` with the characters in `from_str` replaced by the corresponding characters in `to_str`. If to_str is shorter than from_str, it is padded with its last character in order to maintain the correspondence.

Problem

Trying to refactor this into one line to get all vowels in a string to be capitalized. I tried using a hash, but that failed. Still too new at Ruby to know of any alternatives, despite my best efforts to look it up. something like.... `str.gsub!(/aeiou/` ``` def LetterChanges(str) str.gsub!(/a/, "A") if str.include? "a" str.gsub!(/e/, "E") if str.include? "e" str.gsub!(/i/, "I") if str.include? "i" str.gsub!(/o/, "O") if str.include? "o" str.gsub!(/u/, "U") if str.include? "u" puts str end ```

Original source