How to Replace a Hash Key with Another Key

How to replace a hash key with another key

hash[:new_key] = hash.delete :old_key

Replace all HASH keys with another key

If the variable hash holds the hash given in the example, and that object is to be mutated, one can write the following.

hash[:cars].each { |h| h.transform_keys! { |k| k == :seats ? :people : k } }
#=> [
# {
# :name=>"Diablo",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Raptor",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Testarossa",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# }
# ]

​so now

hash
#=> {
# :name=>"Josh",
# :surname=>"Simon",
# :cars=>[
# {
# :name=>"Diablo",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Raptor",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Testarossa",
# :people=>[{:name=>"Josh", :surname=>"Simon"}]
# }
# ]
# }

See Hash#transform_keys!.


If the hash is not to be mutated you could operate on a deep copy. One of doing that is to use the methods Marshal::load and Marshal::dump:

h = Marshal.load(Marshal.dump(hash))
#=> {
# :name=>"Josh",
# :surname=>"Simon",
# :cars=>[
# {
# :name=>"Diablo",
# :seats=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Raptor",
# :seats=>[{:name=>"Josh", :surname=>"Simon"}]
# },
# {
# :name=>"Testarossa",
# :seats=>[{:name=>"Josh", :surname=>"Simon"}]
# }
# ]
# }

Note that if we alter h by writing, for example,

h[:name] = "Lola"
h[:cars][1][:seats][0] = "cats"

we may verify that hash is unchanged.

How to replace a Hash's key names with the values from a separate array?

hash = {field1: 'name', field2: 'street1', field3: 'street2', field4: 'city'}
array = [:address1, :address2, :address3, :city]
hash.each_with_index.map { |(k, v), i| [array[i], v] }.to_h
#=> {:address1=>"name", :address2=>"street1", :address3=>"street2", :city=>"city"}

How to elegantly rename all keys in a hash in Ruby?

ages = { 'Bruce' => 32, 'Clark' => 28 }
mappings = { 'Bruce' => 'Bruce Wayne', 'Clark' => 'Clark Kent' }

ages.transform_keys(&mappings.method(:[]))
#=> { 'Bruce Wayne' => 32, 'Clark Kent' => 28 }

How to change all the keys of a hash by a new set of given keys

Ruby 2.5 has Hash#transform_keys! method. Example using a map of keys

h = {a: 1, b: 2, c: 3}
key_map = {a: 'A', b: 'B', c: 'C'}

h.transform_keys! {|k| key_map[k]}
# => {"A"=>1, "B"=>2, "C"=>3}

You can also use symbol#toproc shortcut with transform_keys Eg:

h.transform_keys! &:upcase
# => {"A"=>1, "B"=>2, "C"=>3}

how to substitute a hash key value in another hash in ruby

You can create a Hash out of a merging all the elements.

hsh = a.inject(:merge)
# => {"a"=>1, "b"=>[10, 9, 11], "c"=>[8, 9], "d"=>[7, 1, 1]}

Then call map on b, just like in Cary's solution.

b.map do |h|
k,v = h.to_a.first
{ k => hsh.key?(v) ? hsh[v] : v }
end
# => [{"k"=>1}, {"l"=>[8, 9]}, {"m"=>1}, {"n"=>[10, 9, 11]}, {"o"=>2}]

How can I replace a single Hash Value in Ruby by using its number

You can do hashOne[hashOne.keys[3]] = 'rubypro'. Also I remember reading that Hashes in ruby are ordered but I'm not entirely sure. (Be careful with using indices because they may not be ordered in other languages) I would ensure that the keys are always returned in the same order before trying to assign a key with an index.

If you really wanted to use an index, I'd recommend using an array because that's not the purpose of a key/value hash.



Related Topics



Leave a reply



Submit