Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Regular Expressions
What will be the output of this Ruby code?
sentence = "one two three four"
result = sentence.scan(/\b\w{3}\b/)
puts result.inspect
A["one", "two"]
B["one", "two", "three", "four"]
C["three", "four"]
D[]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the regex pattern

    /\b\w{3}\b/ matches words exactly 3 letters long.
  2. Step 2: Apply scan to the sentence

    Words "one" and "two" have 3 letters; "three" and "four" do not.
  3. Final Answer:

    ["one", "two"] -> Option A
  4. Quick Check:

    scan with word length 3 = A [OK]
Quick Trick: Use \w{n} to match words of length n [OK]
Common Mistakes:
  • Including words longer than 3 letters
  • Ignoring word boundaries
  • Expecting all words to match

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes