Help walking through this please, hashes with arrays

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]]
Because you're going through each object in the arrays. This code

value[:item_type].each do |item_type|
value[:item_id].each do |item|
value[😛rice].each do |price|
What this is doing is for each item type (0  or 0), do every item (1 & 2), do every price (10 & 14). Now what this is doing is:

Take item type 0. Do:

create_item_object(0, 1, 10)

create_item_object(0, 1, 14)

create_item_object(0, 2, 10)

create_item_object(0, 2, 14)

Then it has to do the other item_type, because you said to do each value, but since it's also 0, it'll be a replica of the above.

Why is it doing this? Because you're going through each item. Item type goes through each item AND each price. Item type 0 goes through every element except the second item type. If you want just something like 0, 1, 10, try something where it will take the same element id from each array.

def create(curr_vocab) SHOPS::SHOPS_HASH.each do |key, value| if key == curr_vocab value[:item_type].size.times { |i| item = create_item_object(value[:item_type], value[:item_id], value[😛rice]) } @inventory.push(item) end puts @inventory.inspect endNow why would this work better? Instead of going through every element in each array for one type, it goes item_type #1, item_id #1, and price #1 go together, item_type #2, item_id #2, price #2 go together. The only thing that this assumes is that the arrays of item_type, item_id, and price are all the same size.

this works by taking i, the current index of the arrays, and looking for the value at that spot in each array. 'i' starts out with the value 0, as do arrays. If you want to change 

create_item_object(value[:item_type], value[:item_id], value[😛rice]

 

into something easier to read, you can just as easily do

type  = value[:item_type]id      = value[:item_id]price = value[😛rice]create_item_object(type, id, price)It works the same way, it just makes it a little more human readable.

 

 

If I wasn't clear on something, please let me know. I feel like this is terrible confusing, personally.
the only thing that confuses me is .size.times  I can deduct that .size is the size of the item_type array but what is times?
when walked through, should create me an array as such: [[0, 1, 10],[0,2,4]]

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 endend
`each` loops over every item in your array.

You are doing a triple nested loop, so you can expect to get more than 2 items (in your case, given those arrays, you should expect 2 x 2 x 2 or 8 elements)

You want to do a single loop and use that to access each element in the separate arrays.

the only thing that confuses me is .size.times I can deduct that .size is the size of the item_type array but what is times?
http://www.ruby-doc.org/core-2.0/Integer.html#method-i-times
@Zalerinian - You need @inventory.push(item) inside your {} or else it spazzes out saying it doesn't know what item is 😉 other then that it works.
I'm just wondering why you're using an array of hashes? Why don't you instead create a class to be representative of your items, and then provide a to_a method if they need to be in the array format? Maybe do something along these lines?

Original thread: https://forums.rpgmakerweb.com/threads/help-walking-through-this-please-hashes-with-arrays.15597/