Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Hash#[]

(K arg0) -> V
hash[key] → value

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
See also .[] #[]= #>= #> also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0