Created
January 12, 2012 00:35
-
-
Save avdi/1597716 to your computer and use it in GitHub Desktop.
Prevent recursion in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Fixing blankety blank blank active blanking record after save callbacks so they don't blanking stack overflow :-)
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
That's pretty ingenius. Do you have a specific use case in mind?