Hashes

Lesson 8 of 20

Key–value pairs the Ruby way: symbol keys, instant lookups, and what happens when a key is missing.

The big picture

Label your data

A hash stores pairs: every key points to exactly one value. You look things up by name, not by position.

Key → value

A hash maps keys to values: { name: "Asha", level: 7 }.

Symbol keys

The shorthand name: creates the Symbol :name — the classic Ruby hash style.

Instant lookup

hero[:level] jumps straight to its value — no scanning, no indexes.

Missing keys give nil

Look up a key that is not there and you get nil — not an error.