From: "Brian F. Feldman" Date: 2001-01-20T17:38:55+09:00 Subject: [ruby-talk:9592] Re: rescue / ensure Bernd Lorbach wrote: > Hi, > > I don't understand what the ensure-clause is for. > If you have > > f = File.open ... > begin > ... > rescue > puts "Error: #{$!}" > ensure > f.close > end > > you just could do > > f = File.open ... > begin > ... > rescue > puts "Error: #{$!}" > end > f.close > > I think the latter one looks more symmetric and does just the same > as the former version, as the rescue clause ensures, that the > program is continued after the begin/end block. > Am I missing something? Yes, ensure "always" does its job: f = File.open ... begin ... return something rescue puts "Error: #{$!}" ensure f.close end The return happens but it passes control to the ensure first, in this example. Fun, huh? -- Brian Fundakowski Feldman \ FreeBSD: The Power to Serve! / green@FreeBSD.org `------------------------------'