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
hash.c
View on GitHub ↗
VALUE
rb_hash_aref(VALUE hash, VALUE key)
{
st_data_t val;
if (hash_stlike_lookup(hash, key, &val)) {
return (VALUE)val;
}
else {
return rb_hash_default_value(hash, key);
}
}
Returns the value associated with the given key, if found:
Example
h = {foo: 0, bar: 1, baz: 2} h[:foo] # => 0
If key is not found, returns a default value (see Default Values):
Example
h = {foo: 0, bar: 1, baz: 2} h[:nosuch] # => nil