“Ruby Hash Merge vs Merge!” Réponses codées

Ruby Hash Merge

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}
h1.merge(h2){|key, oldval, newval| newval - oldval}
               #=> {"a"=>100, "b"=>54,  "c"=>300}
h1             #=> {"a"=>100, "b"=>200}
Hungry Hedgehog

Ruby Hash Merge vs Merge!

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2)   #=> {"a"=>100, "b"=>254, "c"=>300}
h1              #=> {"a"=>100, "b"=>254, "c"=>300}

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2) { |key, v1, v2| v1 }
                #=> {"a"=>100, "b"=>200, "c"=>300}
h1              #=> {"a"=>100, "b"=>200, "c"=>300}
patrick204nqh

Réponses similaires à “Ruby Hash Merge vs Merge!”

Questions similaires à “Ruby Hash Merge vs Merge!”

Plus de réponses similaires à “Ruby Hash Merge vs Merge!” dans Ruby

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code