Skip to content

Instantly share code, notes, and snippets.

@joker1007
Created May 31, 2018 08:00
Show Gist options
  • Save joker1007/56db59e88136e5c2ba0008740439a588 to your computer and use it in GitHub Desktop.
Save joker1007/56db59e88136e5c2ba0008740439a588 to your computer and use it in GitHub Desktop.
class Object
def defer(method, *args)
@current_fibers ||= []
@tracepoint ||= TracePoint.trace(:return) do |tp|
@current_fibers.reverse_each do |fib|
fib.resume
end
end
@current_fibers << Fiber.new do
Fiber.yield
send(method, *args)
end
end
end
def hoge
f = File.new("defer_test.txt", "w")
f.defer(:close)
f.write("hogehoge")
end
hoge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment