Bird
0
0

What happens to the file after the block finishes when you use File.open('file.txt') do |f| ... end in Ruby?

easy📝 Conceptual Q11 of 15
Ruby - File IO
What happens to the file after the block finishes when you use File.open('file.txt') do |f| ... end in Ruby?
AThe file remains open until you manually close it.
BThe file is automatically closed after the block ends.
CThe file is deleted after the block ends.
DThe file is locked and cannot be accessed again.
Step-by-Step Solution
Solution:
  1. Step 1: Understand File.open with block behavior

    When you open a file with a block, Ruby automatically manages the file resource.
  2. Step 2: Recognize automatic closing

    After the block finishes, Ruby closes the file automatically to prevent resource leaks.
  3. Final Answer:

    The file is automatically closed after the block ends. -> Option B
  4. Quick Check:

    File.open with block = auto-close [OK]
Quick Trick: File.open with block always auto-closes the file [OK]
Common Mistakes:
  • Thinking the file stays open after the block
  • Assuming the file is deleted automatically
  • Believing the file locks after block ends

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes