Chapter 11 Iterators

An iterator is a method that allows you to access items one at a time.

Syntax:

method_call do [ | expr... | ] expr...end
    method_call { [ | expr... | ] expr... }

Example:

colors = ["pink", "red", "green", "black"]  
colors.each do |color|
     puts "I like the color " + color 
end

The previous example displays the following output:

I like the color pink
I like the color red
I like the color green
I like the color black

  • Currently 2.97/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
  Flag Inappropriate Content 0 comments