Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Hash#[]=
hash[key] = value → value
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_TYPE(hash) == &identhash || rb_obj_class(key) != rb_cString) {
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
}
else {
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
}
return val;
}
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}