×

ruby · level 1 · unit 6 · lesson 2

  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

Every item has a numbered spot, starting at 0.

Each item sits at a numbered position called its . Ruby starts counting at 0, so the first item is fruit[0]. Count back from the end with minus numbers: fruit[-1] is the last item.

Put a new value at an index to replace that item: fruit[1] = "pear". To a new item onto the end, use << or .push.

Ask for an index with no item there, and Ruby hands back , its word for nothing at all. puts nil shows an empty line.

fruit = ["apple", "banana", "cherry"]
puts fruit[0]
puts fruit[-1]
fruit[1] = "pear"
fruit << "lemon"
p fruit

fruit << "lemon" and fruit.push("lemon") do the same job.