Using gsub and sub for Replacement in Ruby
📖 Scenario: Imagine you have a list of product names with inconsistent formatting. You want to clean up these names by replacing certain words or characters.
🎯 Goal: You will create a Ruby program that uses sub and gsub methods to replace parts of strings. This will help you understand how to change text in Ruby.
📋 What You'll Learn
Create a string variable with a product description
Create a variable to hold the word to replace
Use
sub to replace only the first occurrence of a wordUse
gsub to replace all occurrences of a wordPrint the results to see the changes
💡 Why This Matters
🌍 Real World
Cleaning and formatting text data is common in many jobs, like preparing product descriptions for websites or fixing typos in documents.
💼 Career
Knowing how to replace text efficiently helps in roles like data cleaning, software development, and content management.
Progress0 / 4 steps