I have a problem to solve, There is a simple supermarket program where all items are sorted and stored in a hash(key: Product ID) that has values such as price, manufacturer, expiry etc.
For a purpose, i need to group items according to price.
I need to create a hash (keyed: price) containing another hash (keyed: Product ID).
Code goes like this:
new_Hash = Hash.new(Hash.new)
items = Hash.new
SuperMarket_hash.each { |Product_ID, info | new_Hash[info.PRICE].items[Product_ID] }
I am getting error for this code. (items : NillClass no method error)
Use a vivified hash as shown in “Dynamically creating a multi-dimensional hash in Ruby“
Or use
group_by.