This is just a thought exercise and I'd be interested in any opinions. Although if it works I can think of a few ways I'd use it.
Traditionally, if you wanted to perform a function on the results of a nested loop formed from arrays or ranges etc, you would write something like this:
def foo(x, y)
# Processing with x, y
end
iterable_one.each do |x|
iterable_two.each do |y|
my_func(x, y)
end
end
However, what if I had to add another level of nesting. Yes, I could just add an additonal level of looping. At this point, let's make foo take a variable number of arguments.
def foo(*inputs)
# Processing with variable inputs
end
iterable_one.each do |x|
iterable_two.each do |y|
iterable_three.each do |z|
my_func(x, y, x)
end
end
end
Now, assume I need to add another level of nesting. At this point, it's getting pretty gnarly.
My question, therefore is this: Is it possible to write something like the below?
[iterable_one, iterable_two, iterable_three].nested_each(my_func)
or perhaps
[iterable_one, iterable_two, iterable_three].nested_each { |args| my_func(args) }
Perhaps passing the arguments as actual arguments isn't feasible, could you maybe pass an array to my_func, containing parameters from combinations of the enumerables?
I'd be curious to know if this is possible, it's probably not that likely a scenario but after it occurred to me I wanted to know.