Often, I have a recursive routine within a method that is only called by that method or within the recursive routine itself:
def foo
...
bar
...
end
def bar
...
bar
...
end
But since bar is not used anywhere else, I do not want to define it as a method but somehow put it inside the method that calls it like this:
def foo
...
bar {# some way to mark the recursive routine
...
bar # some way to call the recursive routine
...
}
...
end
Is this possible?
Easy with a lambda/proc:
You can also use a protected or private method.
If performance is a real concern, creating an object (the lambda) each time will be slower, and calling a lambda is also slower, closure and all. My
fruitygem gives me a 3.3x slowdown on this trivial example; the penalty should be much less for cases that actually do something more involved. Just be sure that performance really is an issue; you know what they say about premature optimization…