我認爲你可以這樣做:
2.1.0 :001 > parent_array = [["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]]
=> [["red", "green", "blue"], ["small", "large", "medium"], ["loose", "tight"]]
2.1.0 :002 > first_array = parent_array.shift
=> ["red", "green", "blue"]
2.1.0 :003 > first_array
=> ["red", "green", "blue"]
2.1.0 :004 > parent_array
=> [["small", "large", "medium"], ["loose", "tight"]]
# Use the splat operator (*)
2.1.0 :006 > product = first_array.product(*parent_array)
=> [["red", "small", "loose"], ["red", "small", "tight"], ["red", "large", "loose"], ["red", "large", "tight"], ["red", "medium", "loose"], ["red", "medium", "tight"], ["green", "small", "loose"], ["green", "small", "tight"], ["green", "large", "loose"], ["green", "large", "tight"], ["green", "medium", "loose"], ["green", "medium", "tight"], ["blue", "small", "loose"], ["blue", "small", "tight"], ["blue", "large", "loose"], ["blue", "large", "tight"], ["blue", "medium", "loose"], ["blue", "medium", "tight"]]
2.1.0 :007 > result = product.map {|array| array.join(' ') }
=> ["red small loose", "red small tight", "red large loose", "red large tight", "red medium loose", "red medium tight", "green small loose", "green small tight", "green large loose", "green large tight", "green medium loose", "green medium tight", "blue small loose", "blue small tight", "blue large loose", "blue large tight", "blue medium loose", "blue medium tight"]
這是可能的,是的。問題變成了,你想如何存儲每個數組?你對這些數據做什麼需要分離出來? – 2014-09-04 15:03:47
我想孩子數組保持爲數組,但不是在父母。我試圖在第一個數組上調用'product'並單獨傳遞其餘的數組。 – jmcharnes 2014-09-04 15:04:58
@jmcharnes也許你可以編輯你的問題來詳細解釋這個目標。 (這是完全可能的,但也不是特別困難。) – Ajedi32 2014-09-04 15:07:14