How to add new item to hash

Иван Бишевац picture Иван Бишевац · Mar 5, 2012 · Viewed 315.3k times · Source

I'm new to Ruby and don't know how to add new item to already existing hash. For example, first I construct hash:

hash = {item1: 1}

after that a want to add item2 so after this I have hash like this:

{item1: 1, item2: 2}

I don't know what method to do on hash, could someone help me?

Answer

pjumble picture pjumble · Mar 5, 2012

Create the hash:

hash = {:item1 => 1}

Add a new item to it:

hash[:item2] = 2