Skip to content

Instantly share code, notes, and snippets.

@nobu
Created September 28, 2017 14:16
Show Gist options
  • Save nobu/19b8221f2a9cb87682723cfb82f41796 to your computer and use it in GitHub Desktop.
Save nobu/19b8221f2a9cb87682723cfb82f41796 to your computer and use it in GitHub Desktop.
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