In Ruby, ensure
clause ensures that the code block is executed when an exception is raised or not.
def foo
'foo'
ensure
puts 'ensure'
end
foo # => 'ensure' is output
However, the value in ensure clause is not returned implicitly.
def foo
'foo'
ensure
'ensure'
end
foo # => 'foo', not 'ensure'
You need to return value explicitly with return
.
def foo
'foo'
ensure
return 'ensure'
end
foo # => 'ensure'
Top comments (1)
Rubocop might be against it
rubydoc.info/gems/rubocop/RuboCop/...