When passing a Ruby array as an argument, why does `<<` append while `+=` does not?

前端 未结 2 1558
北荒
北荒 2021-01-24 14:48

I ran across an unexpected result when passing an array to a function and the behavior of << vs +=.

Can anyone explain why the following

2条回答
  •  盖世英雄少女心
    2021-01-24 15:13

    My understanding is that in Ruby all function arguments are passed by reference

    No, as you have discovered yourself, Ruby is pass-by-value, not pass-by-reference. That's why you are seeing the result you are seeing.

提交回复
热议问题