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
How to Update Ruby to 1.9.X on Mac
Turning Long Fixed Number to Array Ruby
Difference Between Gemfile and Gemfile.Lock in Ruby on Rails
Ignoring Gem Because Its Extensions Are Not Built
How to Use Gem to Install Rails on Ubuntu
Rails Sends 0 Byte Files Using Send_File
Purpose of "Consider_All_Requests_Local" in Config/Environments/Development.Rb
Why Aren't Do/End and {} Always Equivalent
Rails 'Parse_Query' Error on Server in Brand New App
How Does Array#Map Have Parameter to Do Something Like This
What Are the Benefits of the New Hash Syntax in Ruby 1.9
Mavericks, Rbenv, Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.1
Gem Install Fails with Openssl Failure
Rails: Is There a Rails Trick to Adding Commas to Large Numbers
Block Comments in HTML.Erb Templates in Rails
Ruby - Share Logger Instance Among Module/Classes
Using Rvm on Ubuntu 12.04 to Use Rails. the Program 'Rails' Is Currently Not Installed