0
0
Rubyprogramming~10 mins

Scan for all matches in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find all words starting with 'a' in the string.

Ruby
text = "apple banana apricot cherry"
matches = text.scan([1])
puts matches
Drag options to blanks, or click blank then click option'
A/\ba\w*/
B/a/
C/^a/
D/a+/
Attempts:
3 left
💡 Hint
Common Mistakes
Using /a/ matches all 'a' characters, not whole words.
Using /^a/ matches only if 'a' is at the start of the string.
2fill in blank
medium

Complete the code to find all numbers in the string.

Ruby
text = "There are 12 apples and 30 bananas."
numbers = text.scan([1])
puts numbers
Drag options to blanks, or click blank then click option'
A/\w+/
B/[a-z]+/
C/\d+/
D/\s+/
Attempts:
3 left
💡 Hint
Common Mistakes
Using /[a-z]+/ matches letters, not numbers.
Using /\s+/ matches spaces, not numbers.
3fill in blank
hard

Fix the error in the code to find all words ending with 'ing'.

Ruby
text = "I am singing and dancing"
matches = text.scan([1])
puts matches
Drag options to blanks, or click blank then click option'
A/\w+ing\b/
B/\bing/
C/ing$/
D/ing\b/
Attempts:
3 left
💡 Hint
Common Mistakes
Using /ing$/ matches only if 'ing' is at the end of the string.
Using /\bing/ matches words starting with 'ing', not ending.
4fill in blank
hard

Fill both blanks to find all words with exactly 4 letters.

Ruby
text = "This code finds four letter words like test and code"
matches = text.scan([1]).select { |word| word.length [2] 4 }
puts matches
Drag options to blanks, or click blank then click option'
A/\b\w+\b/
B==
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or <= selects words longer or shorter than 4, not exactly 4.
5fill in blank
hard

Fill all three blanks to find all words starting with a vowel and longer than 3 letters.

Ruby
text = "apple orange banana egg umbrella cat dog"
matches = text.scan([1]).select { |word| word.length [2] 3 && word[0].downcase [3] /[aeiou]/ }
puts matches
Drag options to blanks, or click blank then click option'
A/\b\w+\b/
B>
C=~
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of =~ for regex match.
Using >= instead of > for length check.