Created
January 29, 2015 05:35
-
-
Save kdmsnr/c49487af5e83ac6f277b to your computer and use it in GitHub Desktop.
metaprogramming ruby sample code: using.rb
This file contains 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
module Kernel | |
def using(resource) | |
begin | |
yield | |
ensure | |
resource.dispose | |
end | |
end | |
def using2(resource) | |
begin | |
yield | |
ensure | |
resource.dispose | |
end | |
end | |
end | |
class Resource | |
def dispose | |
@disposed = true | |
end | |
def disposed? | |
@disposed | |
end | |
end | |
r = Resource.new | |
# using(r){raise} | |
#=> wrong argument type Resource (expected Module) (TypeError) | |
# using2(r){raise} | |
#=> `block in <main>': unhandled exception |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
あ、refinementsか