Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to replace all digits with '#' in the string.
Ruby
text = "Phone: 123-456-7890" result = text.gsub([1], '#') puts result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a regex pattern.
Using incorrect regex syntax like /d/ which matches letter 'd'.
✗ Incorrect
The regex /\d/ matches any digit character. Using it with gsub replaces all digits.
2fill in blank
mediumComplete the code to replace all vowels with '*'.
Ruby
sentence = "Hello World" new_sentence = sentence.gsub([1], '*') puts new_sentence
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of regex.
Not including brackets [] to form a character set.
✗ Incorrect
The regex /[aeiou]/ matches any lowercase vowel. It replaces all vowels with '*'.
3fill in blank
hardFix the error in the code to replace all whitespace with underscores.
Ruby
text = "Ruby is fun" fixed_text = text.gsub([1], '_') puts fixed_text
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of regex.
Not using + to match multiple spaces.
✗ Incorrect
The regex /\s+/ matches one or more whitespace characters, replacing them with underscores.
4fill in blank
hardFill both blanks to replace all digits with 'X' only if digit is greater than 5.
Ruby
numbers = "1234567890" result = numbers.gsub([1]) { |digit| digit.to_i [2] 5 ? 'X' : digit } puts result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect regex or string instead of regex.
Using wrong comparison operator.
✗ Incorrect
The regex /\d/ matches digits. The condition digit.to_i > 5 replaces digits greater than 5 with 'X'.
5fill in blank
hardFill all three blanks to replace all words starting with 'a' or 'A' with 'word'.
Ruby
text = "An apple a day keeps the doctor away" new_text = text.gsub([1]) { |w| w.downcase.start_with?([2]) ? [3] : w } puts new_text
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect regex that doesn't match words.
Checking for uppercase 'A' only.
Not replacing with the correct string.
✗ Incorrect
The regex /\b\w+\b/ matches words. Checking if word starts with 'a' replaces it with 'word'.