Bird
0
0

Find the problem in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Concurrent Programming
Find the problem in this Ruby code snippet:
t = Thread.new { puts "Hello" }
t.join()
puts t.value
AThread.new requires a return statement inside the block.
BCalling t.value before join causes an error.
CThread does not return a value by default, so t.value is nil.
Dt.join() should be called after accessing t.value.
Step-by-Step Solution
Solution:
  1. Step 1: Understand thread return values

    Thread returns the last expression value from its block. Here, puts "Hello" returns nil.
  2. Step 2: Analyze t.value usage

    Calling t.value returns nil because puts returns nil, not an error.
  3. Final Answer:

    Thread does not return a value by default, so t.value is nil. -> Option C
  4. Quick Check:

    Thread value = last expression result (nil here) [OK]
Quick Trick: Thread value is last block expression result, often nil with puts [OK]
Common Mistakes:
  • Expecting t.value to be output string
  • Calling t.value before join (not error but bad practice)
  • Thinking thread blocks must return explicit values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes