215

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

hash = {item1: 1}

After that, I 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?

starball
  • 20,030
  • 7
  • 43
  • 238
Иван Бишевац
  • 13,811
  • 21
  • 66
  • 93

7 Answers7

349

Create the hash:

hash = {:item1 => 1}

Add a new item to it:

hash[:item2] = 2
pjumble
  • 16,880
  • 6
  • 43
  • 51
  • 14
    Use [**merge!**](https://apidock.com/ruby/Hash/merge!) method `hash.merge!(item2: 2)` to **merge** and save the value **!** – maguri Apr 10 '18 at 15:15
  • 5
    @maguri `hash.merge!(item2: 2)` performs slower compared to `hash[:item2] = 2` when there is only one argument – Rahul Dess Oct 05 '18 at 22:00
81

If you want to add new items from another hash - use merge method:

hash = {:item1 => 1}
another_hash = {:item2 => 2, :item3 => 3}
hash.merge(another_hash) # {:item1=>1, :item2=>2, :item3=>3}

In your specific case it could be:

hash = {:item1 => 1}
hash.merge({:item2 => 2}) # {:item1=>1, :item2=>2}

but it's not wise to use it when you should to add just one element more.

Pay attention that merge will replace the values with the existing keys:

hash = {:item1 => 1}
hash.merge({:item1 => 2}) # {:item1=>2}

exactly like hash[:item1] = 2

Also you should pay attention that merge method (of course) doesn't effect the original value of hash variable - it returns a new merged hash. If you want to replace the value of the hash variable then use merge! instead:

hash = {:item1 => 1}
hash.merge!({:item2 => 2})
# now hash == {:item1=>1, :item2=>2}
Alexander
  • 7,484
  • 4
  • 51
  • 65
47

hash.store(key, value) - Stores a key-value pair in hash.

Example:

hash   #=> {"a"=>9, "b"=>200, "c"=>4}
hash.store("d", 42) #=> 42
hash   #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}

Documentation

shilovk
  • 11,718
  • 17
  • 75
  • 74
  • This is my preferred way too. It gets me every time is that it's `hash.store("d", 42)` not `hash.store(d: 42)` as I naturally think it is. – stevec Feb 11 '23 at 05:08
28

It's as simple as:

irb(main):001:0> hash = {:item1 => 1}
=> {:item1=>1}
irb(main):002:0> hash[:item2] = 2
=> 2
irb(main):003:0> hash
=> {:item1=>1, :item2=>2}
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
15

hash[key]=value Associates the value given by value with the key given by key.

hash[:newKey] = "newValue"

From Ruby documentation: http://www.tutorialspoint.com/ruby/ruby_hashes.htm

Connor Leech
  • 18,052
  • 30
  • 105
  • 150
6
hash_items = {:item => 1}
puts hash_items 
#hash_items will give you {:item => 1}

hash_items.merge!({:item => 2})
puts hash_items 
#hash_items will give you {:item => 1, :item => 2}

hash_items.merge({:item => 2})
puts hash_items 
#hash_items will give you {:item => 1, :item => 2}, but the original variable will be the same old one. 
CK5
  • 1,055
  • 3
  • 16
  • 29
-3

Create hash as:

h = Hash.new
=> {}

Now insert into hash as:

h = Hash["one" => 1]
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Ravi Kumar SIngh
  • 311
  • 1
  • 4
  • 17
  • 4
    If you try inserting multiple keys this way, you'll see that you're actually creating a new hash each time. Probably not what you want. And if that _is_ what you want, you don't need the `Hash.new` part regardless, because `Hash[]` is already creating a new hash. – philomory Aug 12 '16 at 01:03