メソッド 値引き渡し

コード

def process1(records)
  records.each do |record|
    record.store("name", "hello")
  end
end

records = [{:age=>35}]
process1(records)
p records

def process2(records)
  records_2 = []
  records.each do |record|
    record_2 = record.clone
    record_2.store("name", "hello")
    records_2 << record_2
  end
end

records = [{:age=>35}]
process2(records)
p records

結果

// process1の場合、引数のrecordsが編集される
[{:age=>35, "name"=>"hello"}]

// process2の場合は、引数が編集されることはない
[{:age=>35}]