I have the following hash:
module SHOPS SHOPS_HASH = { 'GS' => { :item_type => [0, 0], :item_id => [1, 2], 😛rice => [10, 14], } }endIt, when walked through, should create me an array as such: [[0, 1, 10],[0,2,4]]. But it doesn't what I get is:
[[0, 1, 10], [0, [0, 1, 10], 14], [0, 2, 10], [0, [0, 2, 10], 14], [0, 1, 10], [0, [0, 1, 10], 14], [0, 2, 10], [0, [0, 2, 10], 14]]As you can see this array is pretty strange, its not what I wanted.
Now, looking at the code below, your first thought is to say, You can shorten this by doing .... right now, I don't care. I need to see the loops in action when learning a new language, especially ruby. helps me process what's going on. with that said, why is the following producing the above array and not the one I want?
def create(curr_vocab) SHOPS::SHOPS_HASH.each do |key, value| if key == curr_vocab value[:item_type].each do |item_type| value[:item_id].each do |item| value[😛rice].each do |price| item = create_item_object(item_type, item, price) @inventory.push(item) end end end end end puts @inventory.inspect endcreate_item_object(item_type, item_id, price) is designed so that if you pass in 0,1,10 you get an array of [0, 1, 10] back. @inventory is it's own array. the end result would look like: [[0, 1, 10]]
module SHOPS SHOPS_HASH = { 'GS' => { :item_type => [0, 0], :item_id => [1, 2], 😛rice => [10, 14], } }endIt, when walked through, should create me an array as such: [[0, 1, 10],[0,2,4]]. But it doesn't what I get is:
[[0, 1, 10], [0, [0, 1, 10], 14], [0, 2, 10], [0, [0, 2, 10], 14], [0, 1, 10], [0, [0, 1, 10], 14], [0, 2, 10], [0, [0, 2, 10], 14]]As you can see this array is pretty strange, its not what I wanted.
Now, looking at the code below, your first thought is to say, You can shorten this by doing .... right now, I don't care. I need to see the loops in action when learning a new language, especially ruby. helps me process what's going on. with that said, why is the following producing the above array and not the one I want?
def create(curr_vocab) SHOPS::SHOPS_HASH.each do |key, value| if key == curr_vocab value[:item_type].each do |item_type| value[:item_id].each do |item| value[😛rice].each do |price| item = create_item_object(item_type, item, price) @inventory.push(item) end end end end end puts @inventory.inspect endcreate_item_object(item_type, item_id, price) is designed so that if you pass in 0,1,10 you get an array of [0, 1, 10] back. @inventory is it's own array. the end result would look like: [[0, 1, 10]]