Ruby/Hash/replace

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Replace a hash with another key-value pair

counties = { "Three" => 3, "Five" => 5 }
temp = {"Three" => 3 }
counties.replace( temp )



Replace all of the pairs in h with those from another hash

h = {}        # Start with an empty hash
h[:a] = 1     # Map :a=>1.  h is now {:a=>1}
h.store(:b,2) # More verbose: h is now {:a=>1, :b=>2}
h.replace({1=>:a, 2=>;b}  # h is now equal to the argument hash



Replace hash with a constant

counties = { "Three" => 3, "Five" => 5 }
counties.replace( { "Three" => 3 } )