Skip to content

Instantly share code, notes, and snippets.

@avdi
Created January 12, 2012 00:35
Show Gist options
  • Select an option

  • Save avdi/1597716 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

That's pretty ingenius. Do you have a specific use case in mind?

@avdi
Copy link
Copy Markdown
Author

avdi commented Jan 12, 2012

Fixing blankety blank blank active blanking record after save callbacks so they don't blanking stack overflow :-)

@lmarburger
Copy link
Copy Markdown

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