문제

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