0
0
Rubyprogramming~15 mins

Match operator (=~) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Match Operator (=~) in Ruby
📖 Scenario: You are working on a simple text analyzer that checks if certain words appear in sentences. This is useful for filtering messages or searching for keywords.
🎯 Goal: Build a Ruby program that uses the match operator =~ to find if a word exists inside a sentence.
📋 What You'll Learn
Create a variable called sentence with a specific string.
Create a variable called word with a specific string.
Use the match operator =~ to check if word is in sentence.
Print the result of the match operation.
💡 Why This Matters
🌍 Real World
Searching for keywords in text messages, logs, or user input is common in many apps.
💼 Career
Understanding how to find patterns in text is useful for roles in software development, data analysis, and quality assurance.
Progress0 / 4 steps
1
Create the sentence variable
Create a variable called sentence and set it to the string "The quick brown fox jumps over the lazy dog".
Ruby
Need a hint?

Use = to assign the string to sentence.

2
Create the word variable
Create a variable called word and set it to the string "fox".
Ruby
Need a hint?

Assign the string "fox" to the variable word.

3
Use the match operator to find the word
Create a variable called position and set it to the result of sentence =~ /#{word}/ to find the position of word in sentence.
Ruby
Need a hint?

Use =~ with a regular expression containing word inside slashes.

4
Print the match result
Print the value of the variable position using puts.
Ruby
Need a hint?

Use puts position to display the index where the word starts.