ensure, at_exit and cleanup
ensure
ensure is the part of a begin that always runs: after a normal return, after a rescue, and while an exception is still on its way out. The exception is not swallowed; the cleanup happens and the error continues:
begin raise "x" ensure puts :done end
ruby -e 'begin; raise "x"; ensure; puts :done; end'
done
-e:1:in '<main>': x (RuntimeError)
The same works in a method, and it also runs on an early return, which is what makes it reliable for cleanup:
ruby -e 'def f; return 1; ensure; puts :clean; end; p f'
clean
1
A block that closes
The standard library already uses ensure in the methods that open something. File.open (the file API, later in lesson 8.2) with a block closes the file when the block ends, even if it raises:
ruby -e 'File.open("f.txt", "w") { |f| f.puts "x" }; p File.read("f.txt")'
"x\n"
Use the block form even when nothing can fail. The version that assigns f = File.open(...) needs its own ensure with f.close, and the one time the close is forgotten the file stays open until the process ends. The same pattern covers a database connection, a lock and a temporary file.
at_exit
at_exit registers a block that runs when the program ends, after the last line and after the handler for an uncaught exception:
ruby -e 'at_exit { puts "bye" }; puts "hi"'
hi
bye
It is the right place for a summary line or a temporary file that must go away whatever happened. It is the wrong place for anything that matters: the block does not run when the process is killed with kill -9, and several handlers run in reverse order of registration.
Temporary directories
Dir.mktmpdir (the directory API is in lesson 8.3, later) takes a block and removes the directory afterwards, which makes it the cleanest scratch space for a test or a conversion:
ruby -e 'require "tmpdir"; dir = nil; Dir.mktmpdir { |d| dir = d; p File.directory?(d) }; p File.exist?(dir)'
true
false
The directory exists inside the block and is gone after it, even when the block raises. Tempfile from the standard library does the same for one file, and neither needs a name a test has to invent.
What to watch for
A return inside an
ensure overrides the exception or the value the
method was about to return. It is legal, it is almost always a bug, and
Ruby will warn about it in some versions.
ensure is not a place for a long job: it runs
while an exception is in flight, and an error raised there replaces the
original. Keep it to the close, the delete and the unlock.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program write to the standard output?
begin raise "x" ensure puts :done endAnswer
done
ensure runs while the exception is on its way out; the error still reaches the top and ends the program.
-
What does this program print?
def f return 1 ensure puts :clean end p fAnswer
clean / 1
The ensure block runs on the early return, so clean comes before the value p prints.
-
What does this program print?
at_exit { puts "bye" } puts "hi"Answer
hi / bye
at_exit registers a block for the end of the process, after the last line.
-
What does this program print?
require "tmpdir" dir = nil Dir.mktmpdir do |d| dir = d p File.directory?(d) end p File.exist?(dir)Answer
true / false
The directory exists inside the block and is removed after it.
-
When does an
ensureblock run?Answer
After a normal return, after a rescue, and while an error escapes
That is what makes it the place for closing a file or releasing a lock.
-
What does
File.open("f.txt", "w") { |f| f.puts "x" }do with the handle?Answer
Closes it when the block ends, even if the block raises
The block form uses ensure itself, which is why it is the form to write.
-
When does an
at_exitblock not run?Answer
When the process is killed with kill -9
A hard kill gives the process no chance to run any handler.
-
What does this program print?
def f return 1 ensure return 2 end p fAnswer
2
A return inside ensure overrides the value the method was about to return.
-
What does this program print?
File.open("g.txt", "w") { |f| f.puts "x" } p File.read("g.txt")Answer
"x\n"
puts adds a newline, and p shows the string with its quotes and the newline escape.
-
A long job inside
ensureraises its own error. What happens to the original exception?Answer
It is replaced by the new error
An error raised in ensure replaces the one in flight, which is why ensure stays short.
Article author: Arthur Isaev