Defer in Ruby
Here’s the Ruby translation of the Go “Defer” example, formatted in Markdown for Hugo:
Our example demonstrates the use of ensure
in Ruby, which is similar to defer
in other languages. It’s used to ensure that a piece of code is executed, usually for cleanup purposes, regardless of whether an exception is raised or not.
Running the program confirms that the file is closed after being written.
In this Ruby version:
We use a
begin/ensure
block in themain
method to ensure thatclose_file
is called regardless of what happens in thebegin
block.Instead of
panic
, we use Ruby’s exception handling withrescue
to catch and handle errors.The
createFile
function is renamed tocreate_file
to follow Ruby naming conventions.We use
File.new
instead ofos.Create
to create a new file.In
close_file
, we usewarn
to print error messages to STDERR.We don’t need to explicitly return the file object in
create_file
as Ruby automatically returns the last evaluated expression.
This example demonstrates how Ruby’s ensure
can be used to handle cleanup operations, similar to defer
in other languages.