Skip to content

Instantly share code, notes, and snippets.

@Ball
Created September 18, 2009 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ball/189034 to your computer and use it in GitHub Desktop.
Save Ball/189034 to your computer and use it in GitHub Desktop.
class DisposableHero
include System::IDisposable
attr :name
def initialize(name = "Jed")
@name = name
end
def do_something_to(other)
puts "#{@name} washes a car for #{other.name}"
end
def do_something
puts "#{@name} doing something"
end
def dispose
puts "#{@name} is being disposed"
end
end
module System::IDisposable
def self.use_these(*disposers, &block)
block.call(*disposers)
rescue
disposers.each {|d| d.dispose}
end
def using(&block)
block.call(self)
rescue
self.dispose
end
end
DisposableHero.new.using do |d|
d.do_something
end
System::IDisposable.use_these(DisposableHero.new("Tony"), DisposableHero.new("Ryan")) do |t, r|
t.do_something_to(r)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment