Insert new item into array inside hash

I have a hash that has keys that uniquely identify each item in the hash. And inside each element I have an array. So I ask a question how to place another element inside this array inside a hash.

{"Apple"=>[1, 5.99], "Banana"=>[5, 9.99]}

I iterate over the result set and I am a bit lost how to add another element to the array ...

Any help is most appreciated

Greetings

+3
source share
1 answer

If your hash is called, for example, the hshApple array can be accessed via hsh["Apple"]. You can use this as any variable, so adding a value to this array is simple hsh["Apple"] << some_value. For example:

irb> hsh = { "Apple" => [1, 5.99], "Banana" => [5, 9.99] }
irb> hsh["Apple"] << 9999
=> { "Apple" => [1, 5.99, 9999], "Banana" => [5, 9.99] }
+6
source

Source: https://habr.com/ru/post/1728132/


All Articles