0
0
Rubyprogramming~15 mins

Gsub with regex in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Gsub with regex
📖 Scenario: You are working on a text editor feature that needs to clean up user input by replacing certain patterns in a string.
🎯 Goal: Learn how to use Ruby's gsub method with regular expressions to find and replace parts of a text.
📋 What You'll Learn
Create a string variable with a specific sentence.
Create a regular expression pattern to find all vowels.
Use gsub with the regex to replace vowels with a star *.
Print the modified string.
💡 Why This Matters
🌍 Real World
Text editors and input sanitizers often need to find and replace patterns in text, like removing vowels or masking sensitive data.
💼 Career
Understanding how to use regular expressions with string replacement is a valuable skill for software developers working on data cleaning, text processing, and user input validation.
Progress0 / 4 steps
1
Create the initial string
Create a string variable called sentence and set it to the exact value "Hello World! Welcome to Ruby programming."
Ruby
Need a hint?

Use double quotes to create the string exactly as shown.

2
Create a regex pattern for vowels
Create a variable called vowels_regex and set it to a regular expression that matches all vowels (both uppercase and lowercase). Use /[aeiouAEIOU]/ as the pattern.
Ruby
Need a hint?

Use slashes /.../ to create the regex pattern.

3
Replace vowels with stars using gsub
Use the gsub method on sentence with vowels_regex to replace all vowels with the star character '*'. Store the result in a new variable called cleaned_sentence.
Ruby
Need a hint?

Call gsub on sentence with vowels_regex and '*' as arguments.

4
Print the cleaned sentence
Print the variable cleaned_sentence to display the sentence with vowels replaced by stars.
Ruby
Need a hint?

Use puts cleaned_sentence to print the result.