Jon Allured

Computer Programmer, Whiskey Drinker, Comic Book Reader

Grimm on Recursion in Ruby

published 02/27/12

Avdi Grimm had a post on the Code Benders site about preventing recursion in Ruby. He starts by reviewing what recursion is and a typical approach to dealing with it. Grimm has come up with a module that he's been using to wrap a method with a Thread-level flag to know whether its been called already.

He uses Module#instance_method in his module and that got me to reading about the UnboundMethod class, which was interesting. You can use #instance_method to create a new UnboundMethod instance and then associate it with another object with its bind method.

Then the next thing that I wasn't very familiar with was the Thread stuff. Looks like you can use #[] and #[]= as get/set on the thread and return (or set) the values of any thread-local variables.