Today I Learned

A Zero One initiative

Ensure clauses

Today I learned about the ensure clause when raising exceptions. I was having trouble cleaning up certain operations, now within the ensure block the code will always get executed whether an exception is raised or not.

def foo
  begin
     raise Exception, "Oh no"
   ensure
     return "Clean me up Scotty"
  end
end
Looking for help? Each developer at Zero One has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.