×

ruby · level 2 · unit 2 · 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

changes every item and hands back a new array.

Arrays and hashes share a toolbox of methods called . You know one already: .each. This unit adds more, starting with .map. It runs the block on every item and collects each answer into a brand new array.

A short block can go in curly brackets instead of do and end: { |price| price * 2 }. Use curly brackets for one line, and do … end for longer blocks.

.map never changes the original array. Keep the result in a variable if you want to use it later.

prices = [2, 5, 3]
doubled = prices.map { |price| price * 2 }
p doubled
p prices
names = ["ada", "bo"]
p names.map { |name| name.upcase }

The last line of the block is the answer that goes into the new array.