16. Why use the block form File.open(path, "w") { |f| ... }?
- It is faster
- It encrypts the file
- The file is closed automatically when the block ends, even if an error occurs
- It is the only way to write files
Show answer
Answer: The file is closed automatically when the block ends, even if an error occurs
Block forms guarantee resource cleanup.
Lesson: Files, JSON, CSV and Time
18. In CRuby, when do threads improve performance?
- For I/O-bound work such as network or database calls, because the GVL is released while waiting
- For CPU-heavy calculations in pure Ruby
- Never
- Only on Windows
Show answer
Answer: For I/O-bound work such as network or database calls, because the GVL is released while waiting
The GVL limits Ruby code to one thread at a time, but waiting on I/O releases it.
Lesson: Threads, the GVL, Fibers and Ractors