37、外部迭代器

外部迭代器

iterator=9.downto(1)
begin
  print iterator.next while true
  rescue StopIteration
   puts "...blastoff!"
end

987654321...blastof!
=> nil

iterator=9.downto(1)
loop do
   print iterator.next
end
puts "...blastoff!"


module Iterable
  include Enumerable
  def each
      loop { yield self.next }
   end
end


将外部迭代器传递给一个内部迭代器方法
def iterate(iterator)
  loop{yield iterator.next}
end

iterate(9.downto(1)){|x| print x}
987654321=> nil

2011-4-19 15:17 danny

P154

你可能感兴趣的:(迭代器)