Ruby Notes - Hashes

language-features ruby

What is it?

A hash is an association, meaning it is a key/value store where each value has an arbitrary key, and you retrieve the value via that key.

What does it look like?

instrument_section = {
"cello" => "string"
"clarinet" => "woodwind"
}

Anything to remember?

  • Keys must be unique.

  • If a key is not found, it returns nil.

  • You can change the default value using instrument_section = Hash.new(0)