Skip to content

Instantly share code, notes, and snippets.

@kdmsnr
Created January 29, 2015 05:35
Show Gist options
  • Save kdmsnr/c49487af5e83ac6f277b to your computer and use it in GitHub Desktop.
Save kdmsnr/c49487af5e83ac6f277b to your computer and use it in GitHub Desktop.
metaprogramming ruby sample code: using.rb
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
@kdmsnr
Copy link
Author

kdmsnr commented Jan 29, 2015

なんで wrong argument type Resource (expected Module) (TypeError) になるんだろう。
1.9だと大丈夫で、2.xだとおかしい、みたいな?

@kdmsnr
Copy link
Author

kdmsnr commented Jan 29, 2015

あ、refinementsか

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment