Recall & Review
beginner
What does the Ruby method
String#scan do?It searches the string for all occurrences that match a given pattern and returns them as an array.
Click to reveal answer
beginner
How do you use
scan to find all words in a string?Use
scan with a regular expression like /\w+/ to find all word sequences.Click to reveal answer
intermediate
What is the return type of
scan when used with a pattern that has capture groups?It returns an array of arrays, where each inner array contains the captured groups for each match.
Click to reveal answer
intermediate
How can you use a block with
scan?You can pass a block to
scan to process each match as it is found instead of collecting them in an array.Click to reveal answer
beginner
What happens if
scan finds no matches?It returns an empty array.
Click to reveal answer
What does
"hello123".scan(/\d+/) return?✗ Incorrect
The pattern /\d+/ matches one or more digits, so it finds "123".
Which of these returns an array of arrays when using
scan?✗ Incorrect
Capture groups cause
scan to return arrays of the captured parts.What does
"abc123def".scan(/[a-z]+/) return?✗ Incorrect
The pattern matches sequences of letters, so it finds "abc" and "def".
How can you process matches one by one with
scan?✗ Incorrect
Passing a block to
scan lets you handle each match as it is found.What does
"no digits".scan(/\d+/) return?✗ Incorrect
No digits are found, so
scan returns an empty array.Explain how the Ruby
scan method works to find all matches in a string.Think about how you find all parts of a text that fit a pattern.
You got /4 concepts.
Describe how you can use a block with
scan and why it might be useful.Consider what happens if you want to do something with each match immediately.
You got /3 concepts.