Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Hash#[]=

hash[key] = value → value

Hash#store is an alias for Hash#[]=.

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 .[] #[] #any? #>= also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1