Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Hash#store

hash.store(key, value)

Alias of #[]=

Associates the given value with the given key; returns value.

If the given key exists, replaces its value with the given value; the ordering is not affected (see Entry Order):

Example
h = {foo: 0, bar: 1}
h[:foo] = 2 # => 2
h.store(:bar, 3) # => 3
h # => {:foo=>2, :bar=>3}

If key does not exist, adds the key and value; the new entry is last in the order (see Entry Order):

Example
h = {foo: 0, bar: 1}
h[:baz] = 2 # => 2
h.store(:bat, 3) # => 3
h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
See also #[]= #slice #to_a #size also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0