Created
September 18, 2009 12:39
-
-
Save Ball/189034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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