0
0
Rubyprogramming~10 mins

Match operator (=~) 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 check if the string contains the word 'cat'.

Ruby
result = 'concatenate' =~ /[1]/
puts result
Drag options to blanks, or click blank then click option'
Acat
Bdog
Cbat
Drat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a word not present in the string, which returns nil.
2fill in blank
medium

Complete the code to check if the string starts with a digit.

Ruby
result = '7eleven' =~ /^[1]/
puts result
Drag options to blanks, or click blank then click option'
Aa
B\d
Cz
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Using a literal character instead of a digit pattern.
3fill in blank
hard

Fix the error in the code to correctly check if 'hello123' contains digits.

Ruby
result = 'hello123' =~ /[1]/
puts result
Drag options to blanks, or click blank then click option'
A[0-9a-z]
B[a-z]
C\d+
D123
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pattern that matches letters instead of digits.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 4 characters.

Ruby
lengths = Hash[words.select { |word| word.length > 4 }.map { |word| [[1], [2]] }]
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.size
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's len() function instead of Ruby's length method.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys, their lengths as values, only if length is greater than 3.

Ruby
result = Hash[words.select { |word| word.[3] > 3 }.map { |word| [[1], [2]] }]
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
Clength
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's len() or incorrect method names.