Bird
0
0

Which Ruby code correctly replaces all vowels in a string with '#' using string mutability?

hard📝 Application Q8 of 15
Ruby - String Operations
Which Ruby code correctly replaces all vowels in a string with '#' using string mutability?
str = "education"
# Your code here
puts str
Astr.gsub!(/[aeiou]/, '#')
Bstr = str.gsub(/[aeiou]/, '#')
Cstr.replace(str.gsub(/[aeiou]/, '#'))
Dstr.sub(/[aeiou]/, '#')
Step-by-Step Solution
Solution:
  1. Step 1: Understand string mutability methods

    gsub! modifies the string in place, while gsub returns a new string.
  2. Step 2: Analyze options

    str.gsub!(/[aeiou]/, '#') uses gsub! which mutates the original string directly.
  3. Step 3: Compare with other options

    str = str.gsub(/[aeiou]/, '#') reassigns the string, not mutating it in place. str.replace(str.gsub(/[aeiou]/, '#')) replaces the string content but is less direct. str.sub(/[aeiou]/, '#') only replaces the first vowel.
  4. Final Answer:

    str.gsub!(/[aeiou]/, '#') -> Option A
  5. Quick Check:

    Use gsub! for in-place vowel replacement [OK]
Quick Trick: Use gsub! to mutate string in place [OK]
Common Mistakes:
MISTAKES
  • Using gsub instead of gsub! for mutation
  • Using sub which replaces only first match
  • Reassigning string instead of mutating

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes