小编twt*_*ttr的帖子

What the difference between Ruby + and concat for arrays?

I've been trying to collect arrays with digits into one array. If I try to use + it returns emty array as output. Using concat returns expected array of digits. How does it work and what the main difference between these Ruby methods?

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 …
Run Code Online (Sandbox Code Playgroud)

ruby arrays concat step

2
推荐指数
1
解决办法
94
查看次数

标签 统计

arrays ×1

concat ×1

ruby ×1

step ×1