Skip to content

Instantly share code, notes, and snippets.

@unicornrainbow
Forked from avdi/prevent-recursion.rb
Created January 22, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unicornrainbow/8563438 to your computer and use it in GitHub Desktop.
Save unicornrainbow/8563438 to your computer and use it in GitHub Desktop.
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