在David Black的The Well-Grounded Rubyist中,我遇到了以下关于枚举器的Ruby代码:
e = Enumerator.new do |y| puts "Starting up the block!" (1..3).each {|i| y << i } puts "Exiting the block!" end p e.to_a
返回以下输出:
Starting up the block! Exiting the block! [1, 2, 3]
让我困扰的是,我无法围绕执行的顺序.我相信输出应该更直接:
Starting up the block! [1, 2, 3] Exiting the block!
任何帮助将非常感激.