Why this Ruby method passes it's argument by reference
问题 I answered on this question and stumbled on something strange. Ruby passes its arguments by value, the variables itself are references though. So why the first methods seems to pass its parameter by reference ? require 'set' require 'benchmark' def add_item1!(item, list) list << item unless list.include?(item) end def add_item2(item, list) list |= [item] end def add_item3(item, list) set = Set.new(list) set << item list = set.to_a end array1 = [3,2,1,4] add_item1!(5, array1) p array1 # [3, 2,