February 2, 2004 Intro to Ruby Slide #16
Prev Next

Iterators

Iterators are perhaps the feature that most differentiates Ruby from Perl, Python, C++, and its other most well-known ancestors. They're a simple, elegant concept:

     def ntimes(n)
         i = 0
         while i < n
             yield i
             i = i + 1
         end
     end

     ntimes(5) { |x| puts x }


Prev Copyright © 2004 Walter C. Mankowski Next