Recursion is almost always a bad idea for algorithms that can be implemented using another approach. Ruby has a number of problems when working at extreme stack depths such as increasing cost of garbage collection, significant stack overhead, and a relatively low ceiling which may result in "stack level too deep" errors.
You would probably be better off creating independent objects that do not share state. Instead of recursing, iterate. Where applicable, use your own self-managed stack such as an Array you push
and shift
elements from.
Some example code might be:
candidate = stack.shift
if (candidate.fitness > minimum_fitness)
stack.push(candidate.spawn)
end
A method like spawn
would do whatever magical GA things you need to do and return a new, independent instance. This could be augmented to combine with another candidate if required.