×

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

keeps the yeses. drops them.

.select runs a yes-or-no block on every item and keeps the items that get true. .reject is its mirror image: it drops the true items and keeps the rest. Both hand back a new array.

stops at the first item that gets true and hands back that one item, or nil if nothing matches. .count with a block counts the true answers.

On a hash, .select and .reject hand back a smaller hash, and the block gets the key and the value.

ages = [4, 12, 17, 30, 9]
p ages.select { |age| age >= 12 }
p ages.reject { |age| age >= 12 }
p ages.find { |age| age > 15 }
p ages.count { |age| age.even? }

Write each block as a yes-or-no question, and these methods read like plain English.