0
# Call the each method of each collection in turn.
# This is not a parallel iteration and does not require enumerators.
def sequence(*enumerables, &block)
    enumerables.each do |enumerable|
        enumerable.each(&block)
    end
end


# Examples of how these iterator methods work
a,b,c = [1,2,3],4..6,'a'..'e'
print "#{sequence(a,b,c) {|x| print x}}\n"

为什么结果是:

123456abcde[[1, 2, 3], 4..6, "a".."e"]

谁能告诉我为什么 [[1, 2, 3], 4..6, "a".."e"] 被打印出来了?或者告诉我为什么 'sequence' 方法的返回值是 [[1, 2, 3], 4..6, "a".."e"]?? 非常感谢

4

1 回答 1

1
sequence(a,b,c) { |x| print x }

印刷品123456abcde

print "#{some_code}\n"

将打印的返回值some_code。在您的示例中,each循环返回[[1, 2, 3], 4..6, "a".."e"],因为返回值 ifeachself(请参阅:http ://apidock.com/ruby/v1_9_3_392/Enumerator/each )

于 2013-09-11T22:23:13.893 回答