Skip to content

Instantly share code, notes, and snippets.

@unicornrainbow
Forked from avdi/prevent-recursion.rb
Created January 22, 2014 17:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
def self.prevent_recursion(method_name)
flag_name = "in:#{name}##{method_name}"
original = instance_method(method_name)
define_method(method_name) do |*args|
if Thread.current[flag_name]
return
else
begin
Thread.current[flag_name] = true
original.bind(self).call(*args)
ensure
Thread.current[flag_name] = nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment