Skip to content

Instantly share code, notes, and snippets.

@avdi
Created January 12, 2012 00:35
Show Gist options
  • Save avdi/1597716 to your computer and use it in GitHub Desktop.
Save avdi/1597716 to your computer and use it in GitHub Desktop.
Prevent recursion in Ruby
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
@lmarburger
Copy link

Oh. Well that certainly sounds useful. Carry on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment