Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4636

Begin, Rescue and Ensure in Ruby?

$
0
0

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?


Viewing all articles
Browse latest Browse all 4636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>