I was wondering if ensure
was the Ruby equivalent of finally
in C#? Should I have:
file = File.open("myFile.txt", "w")begin file << "#{content} \n"rescue # handle the error hereensure file.close unless file.nil?end
or should I do this?
#store the filefile = File.open("myFile.txt", "w")begin file << "#{content} \n" file.closerescue # handle the error hereensure file.close unless file.nil?end
Does ensure
get called no matter what even if an exception isn't raised?