Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The regex /\ba\w*/ matches words starting with 'a'. The \b ensures word boundary.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using /[a-z]+/ matches letters, not numbers.
Using /\s+/ matches spaces, not numbers.
✗ Incorrect
The regex /\d+/ matches one or more digits, capturing all numbers.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The regex /\w+ing\b/ matches whole words ending with 'ing'.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or <= selects words longer or shorter than 4, not exactly 4.
✗ Incorrect
Use /\b\w+\b/ to find words, then select those with length == 4.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of =~ for regex match.
Using >= instead of > for length check.
✗ Incorrect
Use /\b\w+\b/ to find words, select those longer than 3 and starting with a vowel using =~.