Bird
Raised Fist0
Rubyprogramming~20 mins

Why regex is powerful in Ruby - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Regex Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby regex code?

Look at this Ruby code using regex. What will it print?

Ruby
text = "Hello Ruby 2024!"
match = text.match(/Ruby (\d{4})/)
puts match[1]
A2024
BRuby 2024
CHello
Dnil
Attempts:
2 left
💡 Hint

Remember, parentheses in regex capture groups. What does match[1] return?

🧠 Conceptual
intermediate
1:30remaining
Why is regex powerful in Ruby for text processing?

Choose the best reason why regex is powerful in Ruby.

ARuby requires external libraries to use regex.
BRegex in Ruby only works with strings longer than 10 characters.
CRuby integrates regex directly into its syntax with easy methods like <code>=~</code> and <code>match</code>.
DRuby does not support regex backreferences.
Attempts:
2 left
💡 Hint

Think about how Ruby lets you use regex without extra setup.

🔧 Debug
advanced
2:00remaining
What error does this Ruby regex code raise?

Find the error when running this Ruby code:

Ruby
pattern = /\d{3}-\d{2}-\d{4}/
text = "My number is 123-45-6789"
match = text.match(pattern)
puts match[5]
ASyntaxError
BIndexError
CTypeError
DNoMethodError
Attempts:
2 left
💡 Hint

Check what match[5] means if the regex has fewer capture groups.

📝 Syntax
advanced
2:00remaining
Which option correctly uses regex to find all words starting with 'a' in Ruby?

Choose the Ruby code that correctly finds all words starting with 'a' in a string.

Atext.scan(/\w*a\b/)
Btext.scan(/a\w*\b/)
Ctext.scan(/\b\w*a/)
Dtext.scan(/\ba\w*/)
Attempts:
2 left
💡 Hint

Remember, \b means word boundary and \w* means zero or more word characters.

🚀 Application
expert
2:30remaining
What is the value of 'result' after running this Ruby code?

This Ruby code uses regex with named capture groups. What is the value of result?

Ruby
text = "User: alice, ID: 42"
pattern = /User: (?<name>\w+), ID: (?<id>\d+)/
match = text.match(pattern)
result = {user: match[:name], id: match[:id].to_i}
A{user: "alice", id: 42}
B{:user=>"alice", :id=>42}
C{user: "alice", id: "42"}
D{"user"=>"alice", "id"=>42}
Attempts:
2 left
💡 Hint

Look at how the hash keys and values are created and the use of to_i.

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