Domanda

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
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top