Bird
Raised Fist0
Rubyprogramming~10 mins

Why regex is powerful in Ruby - Test Your Understanding

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

Complete the code to check if the string contains the word 'Ruby'.

Ruby
text = "I love programming in Ruby"
puts text =~ /[1]/ ? "Found" : "Not found"
Drag options to blanks, or click blank then click option'
ARuby
BPython
CJava
DC++
Attempts:
3 left
💡 Hint
Common Mistakes
Using a word not present in the string, causing no match.
2fill in blank
medium

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

Ruby
text = "bat ball cat box"
matches = text.scan(/[1]\w*/)
puts matches.join(", ")
Drag options to blanks, or click blank then click option'
Ad
Bc
Ca
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using a letter that does not match any word start.
3fill in blank
hard

Fix the error in the regex to match an email address.

Ruby
email = "user@example.com"
puts email.match(/\A[\w.+-]+@[1]\.[a-z]{2,}\z/i) ? "Valid" : "Invalid"
Drag options to blanks, or click blank then click option'
A[a-z]+
B[0-9]+
C[a-z0-9-]+
D[A-Z]+
Attempts:
3 left
💡 Hint
Common Mistakes
Using only letters or digits, missing hyphens in domain.
4fill in blank
hard

Fill both blanks to create a regex that matches a date in 'YYYY-MM-DD' format.

Ruby
date = "2024-06-15"
pattern = /\A[1]-[2]-\d{2}\z/
puts date.match?(pattern) ? "Date valid" : "Date invalid"
Drag options to blanks, or click blank then click option'
A\d{4}
B\d{2}
C[a-z]{4}
D[0-9]{3}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong digit counts or letters instead of digits.
5fill in blank
hard

Fill all three blanks to create a hash that maps words to their lengths only if length is greater than 3.

Ruby
words = ["ruby", "is", "fun", "and", "powerful"]
lengths = { [1]: [2] for [3] in words if [2] > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or missing the length condition.

Practice

(1/5)
1. Why is regex considered powerful in Ruby?
easy
A. It cannot be combined with other Ruby methods.
B. It only works with numbers, not text.
C. It requires complex syntax to use.
D. It can quickly find and manipulate text patterns.

Solution

  1. Step 1: Understand regex purpose in Ruby

    Regex is used to find patterns in text efficiently.
  2. Step 2: Recognize Ruby's regex flexibility

    Ruby allows easy pattern matching and text manipulation using regex.
  3. Final Answer:

    It can quickly find and manipulate text patterns. -> Option D
  4. Quick Check:

    Regex power = quick text pattern handling [OK]
Hint: Regex helps find text patterns fast in Ruby [OK]
Common Mistakes:
  • Thinking regex only works with numbers
  • Believing regex syntax is always complex
  • Assuming regex can't be combined with Ruby methods
2. Which of the following is the correct way to write a regex literal in Ruby?
easy
A. /pattern/
B. 'pattern'
C. {pattern}
D. (pattern)

Solution

  1. Step 1: Identify Ruby regex syntax

    Ruby uses slashes / / to define regex literals.
  2. Step 2: Compare options

    Only /pattern/ matches Ruby regex literal syntax.
  3. Final Answer:

    /pattern/ -> Option A
  4. Quick Check:

    Regex literal = /pattern/ [OK]
Hint: Regex literals in Ruby use slashes /pattern/ [OK]
Common Mistakes:
  • Using quotes instead of slashes
  • Using braces or parentheses for regex
  • Confusing regex with string syntax
3. What will be the output of this Ruby code?
text = "hello123"
match = text.match(/\d+/)
puts match[0]
medium
A. hello
B. 123
C. nil
D. error

Solution

  1. Step 1: Understand the regex /\d+/

    This regex matches one or more digits in the string.
  2. Step 2: Apply regex to text "hello123"

    The digits "123" match the pattern and are captured.
  3. Final Answer:

    123 -> Option B
  4. Quick Check:

    Digits found = 123 [OK]
Hint: \d+ matches digits; here it finds '123' [OK]
Common Mistakes:
  • Expecting 'hello' as output
  • Thinking match returns nil if partial match exists
  • Confusing match object with string
4. Find the error in this Ruby code using regex:
text = "abc123"
if text =~ /\d+/
  puts "Digits found"
else
  puts "No digits"
end
medium
A. The regex syntax is incorrect
B. The =~ operator is used wrongly
C. No error; code works as expected
D. Missing parentheses in if condition

Solution

  1. Step 1: Check regex syntax

    The regex /\d+/ correctly matches digits.
  2. Step 2: Verify use of =~ operator

    The =~ operator checks if regex matches the string; usage is correct.
  3. Final Answer:

    No error; code works as expected -> Option C
  4. Quick Check:

    Regex and operator usage = correct [OK]
Hint: =~ checks regex match; no parentheses needed [OK]
Common Mistakes:
  • Thinking =~ is invalid operator
  • Expecting parentheses required in if
  • Assuming regex syntax is wrong
5. How can you use regex in Ruby to replace all vowels in a string with '*'?
hard
A. string.gsub(/[aeiou]/i, '*')
B. string.replace(/[aeiou]/, '*')
C. string.sub(/[aeiou]/, '*')
D. string.match(/[aeiou]/).replace('*')

Solution

  1. Step 1: Identify method to replace all matches

    gsub replaces all occurrences matching regex in the string.
  2. Step 2: Use regex for vowels with case-insensitive flag

    /[aeiou]/i matches all vowels regardless of case.
  3. Step 3: Combine to replace vowels with '*'

    string.gsub(/[aeiou]/i, '*') replaces all vowels with '*'.
  4. Final Answer:

    string.gsub(/[aeiou]/i, '*') -> Option A
  5. Quick Check:

    Replace vowels = gsub with /[aeiou]/i [OK]
Hint: Use gsub with /[aeiou]/i to replace all vowels [OK]
Common Mistakes:
  • Using sub replaces only first match
  • Using replace method incorrectly
  • Trying to call replace on match object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard