📖 Scenario: Imagine you have a paragraph of text and you want to find all the words that start with the letter 'a'. This is useful when you want to highlight or count specific words in a text.
🎯 Goal: You will write a Ruby program that uses the scan method to find all words starting with the letter 'a' in a given string.
📋 What You'll Learn
Create a string variable called text with the exact sentence: "An apple a day keeps the doctor away."
Create a regular expression variable called pattern that matches words starting with the letter 'a' (case insensitive).
Use the scan method on text with pattern to find all matching words and store them in a variable called matches.
Print the matches array.
💡 Why This Matters
🌍 Real World
Finding all matches in text is useful for searching, filtering, or analyzing data like emails, logs, or documents.
💼 Career
Many programming jobs require text processing skills, including using regular expressions to extract information efficiently.
Progress0 / 4 steps
1
Create the text string
Create a string variable called text and set it to the exact sentence: "An apple a day keeps the doctor away."
Ruby
Hint
Use double quotes around the sentence and assign it to text.
2
Create the pattern to match words starting with 'a'
Create a regular expression variable called pattern that matches words starting with the letter 'a' or 'A'. Use /\b[aA]\w*/ as the pattern.
Ruby
Hint
The pattern /\b[aA]\w*/ matches words starting with 'a' or 'A'.
3
Find all matches using scan
Use the scan method on text with pattern to find all matching words and store them in a variable called matches.
Ruby
Hint
Use text.scan(pattern) to get all words starting with 'a' or 'A'.
4
Print the matches
Print the matches array using puts matches.inspect to show all matched words.
Ruby
Hint
Use puts matches.inspect to print the array of matches.
Practice
(1/5)
1. What does the Ruby method scan do when used on a string?
easy
A. It counts the number of characters in the string.
B. It replaces all matches of a pattern with another string.
C. It finds all matches of a pattern and returns them in an array.
D. It converts the string to uppercase.
Solution
Step 1: Understand the purpose of scan
The scan method searches the string for all occurrences of a pattern.
Step 2: Identify the return value of scan
It returns an array containing all matched parts of the string.
Final Answer:
It finds all matches of a pattern and returns them in an array. -> Option C
Quick Check:
scan returns matches array [OK]
Hint: Remember: scan returns all matches as an array [OK]
Common Mistakes:
Confusing scan with gsub (replacement)
Thinking scan returns a count
Assuming scan modifies the original string
2. Which of the following is the correct syntax to find all digits in a string str using scan?
easy
A. str.scan(/d+)
B. str.scan("\\d+")
C. str.scan('\d+')
D. str.scan(/\d+/)
Solution
Step 1: Recognize the correct regex syntax in Ruby
Ruby uses forward slashes /pattern/ to denote regular expressions.
Step 2: Identify the correct regex for digits
\d+ matches one or more digits. The pattern must be inside slashes for regex.
Final Answer:
str.scan(/\d+/) -> Option D
Quick Check:
Regex in slashes with \d+ = str.scan(/\d+/) [OK]
Hint: Use /pattern/ for regex, not strings, in scan [OK]
Common Mistakes:
Using string quotes instead of regex slashes
Missing escape backslash in regex
Using incorrect regex delimiters
3. What is the output of the following Ruby code?
text = "cat bat rat"
matches = text.scan(/.at/)
puts matches.inspect
medium
A. ["at", "at", "at"]
B. ["cat", "bat", "rat"]
C. ["c", "b", "r"]
D. []
Solution
Step 1: Understand the regex /.at/
The dot . matches any single character, so .at matches any three-letter word ending with 'at'.
Step 2: Apply scan on the string
The string "cat bat rat" contains "cat", "bat", and "rat" which all match the pattern.
Final Answer:
["cat", "bat", "rat"] -> Option B
Quick Check:
scan finds all 3-letter words ending with 'at' [OK]
Hint: Dot matches any char; scan returns all full matches [OK]
Common Mistakes:
Thinking dot matches zero or multiple chars
Expecting partial matches like 'at' only
Confusing scan output with string split
4. The following code is intended to find all words starting with 'a' in sentence. What is the error?
The scan method expects a regex pattern, not a plain string.
Step 2: Identify correct regex usage
The pattern should be written as /a\w+/ to match words starting with 'a'.
Final Answer:
The pattern should be a regex, not a string. -> Option A
Quick Check:
scan requires regex pattern, not string [OK]
Hint: Use /pattern/ for regex in scan, not string quotes [OK]
Common Mistakes:
Passing string instead of regex to scan
Misunderstanding backslash escaping
Assuming scan modifies the string
5. Given the string data = "log12 ID123, ID456, ID789", how can you use scan to extract only the numeric parts after each 'ID' (e.g., ["123", "456", "789"])?
hard
A. data.scan(/ID(\d+)/).flatten
B. data.scan(/\d+/)
C. data.scan(/ID\d+/)
D. data.scan(/(ID\d+)/)
Solution
Step 1: Understand the pattern to extract numbers after 'ID'
The regex /ID(\d+)/ matches 'ID' followed by digits, capturing only digits in parentheses. This ignores other digits like '12' in 'log12'.
Step 2: Use scan and flatten the result
scan returns an array of arrays for capture groups, so flatten converts it to a simple array of strings.
Final Answer:
data.scan(/ID(\d+)/).flatten -> Option A
Quick Check:
Capture group extracts digits, flatten simplifies array [OK]
Hint: Use capture groups and flatten to get inner matches [OK]
Common Mistakes:
Using data.scan(/\d+/) which grabs all digits including extras like '12' and '78'
Using scan without flatten, getting nested arrays
Matching full 'ID123' instead of just digits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.