Skip to content

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

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.4 · 3.3 · 3.2 · 3.1