0
0
Rubyprogramming~5 mins

Scan for all matches in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A["h", "e", "l", "l", "o"]
B["hello"]
C[]
D["123"]
Which of these returns an array of arrays when using scan?
AUsing a pattern without capture groups
BUsing a pattern with capture groups
CUsing <code>scan</code> with no arguments
DUsing <code>scan</code> on an empty string
What does "abc123def".scan(/[a-z]+/) return?
A["abc", "def"]
B["abc123def"]
C["123"]
D[]
How can you process matches one by one with scan?
ABy passing a block to <code>scan</code>
BBy calling <code>each</code> on the string
CBy using <code>match</code> instead
DBy converting the string to an array first
What does "no digits".scan(/\d+/) return?
A["0"]
B["no", "digits"]
C[]
Dnil
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.