End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Hash#store
hash.store(key, value)
hash.c
View on GitHub ↗
VALUE
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
{
int iter_lev = RHASH_ITER_LEV(hash);
rb_hash_modify(hash);
if (RHASH_TABLE_NULL_P(hash)) {
if (iter_lev > 0) no_new_key();
ar_alloc_table(hash);
}
if (RHASH_TYPE(hash) == &identhash || rb_obj_class(key) != rb_cString) {
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
}
else {
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
}
return val;
}
Alias of #[]=
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}