Tag: 深度

查找可变数量的Ruby数组的产品

我希望从可变数量的数组中找到单个项目的所有组合。 我如何在Ruby中执行此操作? 给定两个数组,我可以像这样使用Array.product: groups = [] groups[0] = [“hello”, “goodbye”] groups[1] = [“world”, “everyone”] combinations = groups[0].product(groups[1]) puts combinations.inspect # [[“hello”, “world”], [“hello”, “everyone”], [“goodbye”, “world”], [“goodbye”, “everyone”]] 当组包含可变数量的数组时,此代码如何工作?