Skip to content

Instantly share code, notes, and snippets.

@brianstorti
Created October 31, 2011 02:07
Show Gist options
  • Save brianstorti/1326749 to your computer and use it in GitHub Desktop.
Save brianstorti/1326749 to your computer and use it in GitHub Desktop.
using
require 'test/unit'
module Kernel
def using(resource)
begin
yield if block_given?
ensure
resource.dispose
end
end
end
class TestUsing < Test::Unit::TestCase
class Resource
def dispose
@disposed = true
end
def disposed?
@disposed
end
end
def test_disposes_of_resource
r = Resource.new
using(r) {}
assert r.disposed?
end
def test_disposes_of_resource_in_case_of_exception
r = Resource.new
assert_raises(Exception) do
using(r) { raise Exception }
end
assert r.disposed?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment