Yes, ensure ensures that the code is always evaluated. That's why it's called ensure. So, it is equivalent to Java's and C#'s finally.
The general flow of begin/rescue/else/ensure/end looks like this:
点击(此处)折叠或打开
- begin
- # something which might raise an exception
- rescue SomeExceptionClass => some_variable
- # code that deals with some exception
- rescue SomeOtherException => some_other_variable
- # code that deals with some other exception
- else
- # code that runs only if *no* exception was raised
- ensure
- # ensure that this code always runs, no matter what
- end