Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Hash#store
(K key, V value) -> V
store(p1, p2)
hash.c
View on GitHub ↗
VALUE
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
{
bool iter_p = hash_iterating_p(hash);
rb_hash_modify(hash);
if (!RHASH_STRING_KEY_P(hash, key)) {
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
}
else {
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
}
return val;
}
Alias of #[]=
Associates the given object with the given key; returns object.
Searches for a hash key equivalent to the given key; see Hash Key Equivalence.
If the key is found, replaces its value with the given object; the ordering is not affected (see Entry Order):
Example
h = {foo: 0, bar: 1} h[:foo] = 2 # => 2 h[:foo] # => 2
If key is not found, creates a new entry for the given key and object; the new entry is last in the order (see Entry Order):
Example
h = {foo: 0, bar: 1} h[:baz] = 2 # => 2 h[:baz] # => 2 h # => {foo: 0, bar: 1, baz: 2}
Related: []; see also Methods for Assigning.