Bird
0
0

You want to debug a Ruby method that sometimes returns nil. How can you use binding.pry to pause execution only when the variable output is nil?

hard📝 Application Q8 of 15
Ruby - Ecosystem and Best Practices
You want to debug a Ruby method that sometimes returns nil. How can you use binding.pry to pause execution only when the variable output is nil?
AAdd <code>binding.pry if output == false</code> to pause when <code>output</code> is false.
BUse <code>binding.pry unless output.nil?</code> to pause when <code>output</code> is not nil.
CPlace <code>binding.pry</code> at the start of the method unconditionally.
DInsert <code>binding.pry if output.nil?</code> to conditionally start Pry only when <code>output</code> is nil.
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional breakpoints

    Using binding.pry if output.nil? pauses only when output is nil.
  2. Step 2: Evaluate other options

    Use binding.pry unless output.nil? to pause when output is not nil. pauses when output is not nil, which is opposite. Place binding.pry at the start of the method unconditionally. pauses always. Add binding.pry if output == false to pause when output is false. pauses on false, not nil.
  3. Final Answer:

    Insert binding.pry if output.nil? to conditionally start Pry only when output is nil. -> Option D
  4. Quick Check:

    Does the breakpoint trigger only on nil? Yes [OK]
Quick Trick: Use conditional binding.pry with if output.nil? [OK]
Common Mistakes:
  • Using unless instead of if for nil check
  • Pausing unconditionally
  • Checking for false instead of nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes