Ruby hash merge (Add element to Ruby hash)
This sample shows how to add element to ruby hash
This sample shows how to add element to ruby hash
The following piece of code insert to each hash in the array a new element. I used destructive hash merge to add the new element to each hash in the array.
h = {”boy” => 1, “apple” => 2, “cat” => 3}
Will output:
cat => 3
boy => 1
apple => 2
To get sorted ruby hash output, do:
Will output:
apple => 2
boy => 1
cat => 3