Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I keep the hash associations when building permutations
    text
    copied!<p>UPDATE: Possible solution at the end, but certainly not performant or ideal.</p> <p>I created the following method that gives me close to what I want.</p> <pre><code>def multi_permutations(collection) case collection.length when 1 return collection.shift[1] when 0 raise "You must pass in a multidimensional collection." end a = collection.shift[1] b = multi_permutations(collection) return_value = [] a.each do |a_value| b.each do |b_value| return_value &lt;&lt; [a_value] + [b_value] end end return return_value end </code></pre> <p>When I pass in a hash with nested arrays that looks like this...</p> <pre><code>my_collection["item_9"] = [152] my_collection["item_2"] = [139, 143, 145] my_collection["item_13"] = [138, 142, 150] my_collection["item_72"] = [137, 149, 151, 154] my_collection["item_125"] = [140, 141] my_collection["item_10"] = [144, 146, 147, 148, 153] </code></pre> <p>I want it to create an array of hashes with all permutations that looks like this...</p> <pre><code>[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }] [{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }] . . . [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }] [{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }] </code></pre> <p>What this function ends up doing is close but I lose the relationships.</p> <pre><code>[152, [139, [138, [137, [140, 144]]]]] [152, [139, [138, [137, [140, 146]]]]] [152, [139, [138, [137, [140, 147]]]]] [152, [139, [138, [137, [140, 148]]]]] [152, [139, [138, [137, [140, 153]]]]] . . . [152, [145, [150, [154, [141, 144]]]]] [152, [145, [150, [154, [141, 146]]]]] [152, [145, [150, [154, [141, 147]]]]] [152, [145, [150, [154, [141, 148]]]]] [152, [145, [150, [154, [141, 153]]]]] </code></pre> <p>The relationships are very important to me. The reason is, I plan to hydrate an object where the attrs are the keys in the hash. I'm sure this can be done in a better way, and I'm open to suggestions.</p> <p>So one possible solution I've come up with is to create a keys array then flatten the permutations and zip them together in a hash.</p> <pre><code> results = [] permutations = multi_permutations(possibilities) permutations.each do |permutation| results &lt;&lt; Hash[keys.zip permutation.flatten!] end </code></pre> <p>This ends up giving me...</p> <pre><code>{"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;146} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;147} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;148} {"item_9"=&gt;152, "item_2"=&gt;145, "item_13"=&gt;150, "item_72"=&gt;154, "item_125"=&gt;141, "item_10"=&gt;153} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload