Skip to Content
Data Structures
Iterating Over Arrays

Iterating over arrays is easier than it looks.

numbers = [1, 2, 3, 4, 5] numbers.each { |element| puts element }
  1. In the example above, we create an array called numbers with 5 elements.
  2. Then we say, “Take this array and for each element, print it to the console.” As usual, we can use any placeholder name for the bit between two | | characters.



Use the .each iterator to puts out each element of the languages array.

Make sure to use puts instead of print, so each element is on its own line.

Folder Icon

Take this course for free

Already have an account?