Loops & Iterators

The .each Iterator

Great work! You're really getting the hang of this.

The loop iterator is the simplest, but also one of the least powerful. A more useful iterator is the .each method, which can apply an expression to each element of an object, one at a time. The syntax looks like this:

object.each { |item| 
  # Do something 

You can also use the do keyword instead of {}:

object.each do |item| 
  # Do something 

The variable name between | | can be anything you like: it's just a placeholder for each element of the object you're using .each on.


Take a look at the code in the editor. When you're pretty sure you know what it'll do, click Run to test your theory.

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.