Created
September 28, 2017 14:16
-
-
Save nobu/19b8221f2a9cb87682723cfb82f41796 to your computer and use it in GitHub Desktop.
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 WithResources | |
def self.with(release_method: :close, &block) | |
vars = [] | |
trace = TracePoint.new(:b_call) do |tp| | |
trace.disable | |
b = tp.binding | |
block.parameters.each do |_, name| | |
vars << [name, b.local_variable_get(name)] | |
end | |
end | |
trace.enable | |
yield | |
ensure | |
trace.disable | |
vars.reverse_each do |v| | |
p v | |
end | |
end | |
end | |
WithResources.with do | |
|a = 1, b = (a + 1)| | |
p [a, b] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment