×

ruby · level 2 · unit 1 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A looks values up by name, not by number.

An array finds items by their position. A hash finds them by name, like the price list in a café. Each name is a , and each key points to its value. A key and its value together make a .

Write a hash with curly brackets. Inside, put each pair as key => value, with commas between the pairs. The => arrow points from the key to its value.

To look something up, put the key in square brackets: menu["tea"]. Ask for a key that is not there, and you get nil.

menu = { "tea" => 2, "cake" => 3, "soup" => 4 }
puts menu["cake"]
p menu
puts menu.length
p menu["pizza"]

p shows the whole hash with its curly brackets, and .length counts the pairs.