0

I need something like this below.

products['first_category'][0] = ['aaa', 'bb', 'ccc']
products['first_category'][1] = ['aa', 'bb', 'cc']
products['first_category'][2] = ['a', 'b', 'c']
products['second_category'][0] = ['asd', 'sdfb', 'csdfd']
products['second_category'][1] = ['sdf', 'bsdf', 'dsfssd']
all_products = products['first_category'] + products['second_category']

This should produce an array of the form

all_products[0] = ['aaa', 'bb', 'ccc']
all_products[1] = ['aa', 'bb', 'cc']
all_products[2] = ['a', 'b', 'c']
all_products[3] = ['asd', 'sdfb', 'csdfd']
all_products[4] = ['sdf', 'bsdf', 'dsfssd']

But I don't know how to implement this in ruby. I tried this but it gives error.

1
  • 1
    "it gives error" - please include the error. Commented Dec 23, 2015 at 12:09

3 Answers 3

2
▶ products = {}
#⇒ {}
▶ products['first_category'] = []
#⇒ []
▶ products['second_category'] = []
#⇒ []
▶ products['first_category'][0] = ['aaa', 'bb', 'ccc']
▶ products['first_category'][1] = ['aa', 'bb', 'cc']
▶ products['first_category'][2] = ['a', 'b', 'c']
▶ products['second_category'][0] = ['asd', 'sdfb', 'csdfd']
▶ products['second_category'][1] = ['sdf', 'bsdf', 'dsfssd']
▶ all_products = products['first_category'] + products['second_category']
#⇒ [
#  [0] [
#    [0] "aaa",
#    [1] "bb",
#    [2] "ccc"
#  ],
#  [1] [
#    [0] "aa",
#    [1] "bb",
#    [2] "cc"
#  ],
#  [2] [
#    [0] "a",
#    [1] "b",
#    [2] "c"
#  ],
#  [3] [
#    [0] "asd",
#    [1] "sdfb",
#    [2] "csdfd"
#  ],
#  [4] [
#    [0] "sdf",
#    [1] "bsdf",
#    [2] "dsfssd"
#  ]
# ]

The tricks here are:

  1. Since your top-level indices are strings, the object itself must be a hash rather than an array. Arrays might have integer indices only.
  2. All subarrays must be explicitly created before one might add elements to them.
Sign up to request clarification or add additional context in comments.

Comments

1

Your code is fine. Just use below line to create products (add it to top of your code snippet)

products = Hash.new {|hash, key| hash[key] = [] }

You need to use Hash as Array can't have non-numeric indices. Hash allows you to accepts its elements in array-like syntax even though it's a dictionary. In addition, the above code will ensure that if there is no entry for a given key, an empty array is used as its value and added to hash.


Working sample:

products = Hash.new {|hash, key| hash[key] = [] }

products['first_category'][0] = ['aaa', 'bb', 'ccc']
products['first_category'][1] = ['aa', 'bb', 'cc']
products['first_category'][2] = ['a', 'b', 'c']

products['second_category'][0] = ['asd', 'sdfb', 'csdfd']
products['second_category'][1] = ['sdf', 'bsdf', 'dsfssd']

all_products = products['first_category'] + products['second_category']

p all_products[0]
#=> ["aaa", "bb", "ccc"]
p all_products[1]
#=> ["aa", "bb", "cc"]
p all_products[2]
#=> ["a", "b", "c"]
p all_products[3]
#=> ["asd", "sdfb", "csdfd"]
p all_products[4]
#=> ["sdf", "bsdf", "dsfssd"]

Comments

0

just try

all_products = products.values.flatten(1)

You take the values into an array ignoring the keys and flatten it to the level of 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.