Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Hash#[]
(K arg0) -> V
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