What is the difference between puts a1.zip(a2) and a1.zip(a2)? Why do the outputs come out in different way?

a1 = %w{a b c}
a2 = %w{1 2 3}

a1.zip(a2)
# => [["a", "1"], ["b", "2"], ["c", "3"]]

puts a1.zip(a2)
# =>
# a
# 1
# b
# 2
# c
# 3
#=> nil
有帮助吗?

解决方案

You are doing puts in one and just the zip call in the other. Why do you expect them to be the same?

puts does print each element of an array in its own line, which is what you see here. IRB by default uses inspect to print objects. While what you see is different, the output from the zip method in both the cases is the same.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top