Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Hashクラス > update
update(other) -> self[permalink][rdoc]update(other) {|key, self_val, other_val| ... } -> selfselfとotherのハッシュの内容をマージ(統合)します。Hash#merge!と同じです。
デフォルト値はselfの設定のままです。 otherがハッシュではない場合、otherのメソッドto_hashを使って暗黙の変換を試みます。
foo = {1 => 'a', 2 => 'b', 3 => 'c'}
bar = {2 => 'B', 3 => 'C', 4 => 'D'}
p foo.update(bar) #=> {1=>"a", 2=>"B", 3=>"C", 4=>"D"}
p foo #=> {1=>"a", 2=>"B", 3=>"C", 4=>"D"}
p foo.update(bar) {|key, foo_val, bar_val| foo_val + bar_val } #=> {1=>"a", 2=>"BB", 3=>"CC", 4=>"DD"}
p foo #=> {1=>"a", 2=>"BB", 3=>"CC", 4=>"DD"}
[SEE_ALSO] Hash#merge!