One of the things that I usually use in ruby is recursion patterns. For example, suppose I have an array that can contain arrays as elements of unlimited depth. So for example:
my_array = [1, [2, 3, [4, 5, [6, 7]]]]
I would like to create a method that can smooth an array into [1, 2, 3, 4, 5, 6, 7].
I know that I .flattenwill fulfill this task, but this problem is intended as an example of recursion problems that I regularly encounter - and therefore I am trying to find a more reusable solution.
In short - I guess there is a standard template for this kind of thing, but I can't think of anything particularly elegant. Any ideas appreciated
source
share