Archive for Ruby Hash

Ruby hash merge (Add element to Ruby hash)

This sample shows how to add element to ruby hash


Comments (4)

Insert into Ruby Array

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.


Comments (3)

Iterate on Ruby hash (sorted and unsorted)

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


Comments