我一直在尝试将带有数字的数组收集到一个数组中。如果我尝试使用 + 它会返回 emty 数组作为输出。使用 concat 返回预期的数字数组。它是如何工作的,这些 Ruby 方法之间的主要区别是什么?
0.step.with_object([]) do |index, output|
output + [index]
break output if index == 100
do # returns empty array
0.step.with_object([]) do |index, output|
output.concat [index]
break output if index == 100
end # returns an array contains digits from 0 to 100