How to Elegantly Rename All Keys in a Hash in Ruby

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 rename a key in a hash if it exists


a[:test] = a.delete(:foo) if a.key?(:foo)

How to replace a hash key with another key


hash[:new_key] = hash.delete :old_key

Is there an elegant way to remove a specific key from a hash and it's sub-hashes in Ruby

I don't think there is a built in method for this so a simple recursive method along the following lines is one solution:

def recursive_delete(hash, to_remove)
hash.delete(to_remove)
hash.each_value do |value|
recursive_delete(value, to_remove) if value.is_a? Hash
end
end

With your example data:

h = { :action => "index", :controller => "home", :secret => "I love Jeff Atwood",
:user => {:name => "Steve", :secret => "I steal Joel's pants"}}

recursive_delete(h, :secret)

puts h.inspect

Gives:

{:controller=>"home", :user=>{:name=>"Steve"}, :action=>"index"}

Note that this solution works in place i.e. it is modifying the original Hash, not returning a new Hash with the requested key excluded.

How do I add a prefix to each key in a hash?


hsh1 = {'address' => "foo", 'postcode' => "bar"}
hsh2 = Hash[hsh1.map{|k,v| [k.dup.prepend("shipping_"),v]}]
p hsh2
# >> {"shipping_address"=>"foo", "shipping_postcode"=>"bar"}

update

hsh1 = {'address' => "foo", 'postcode' => "bar"}
hsh2 = Hash[hsh1.map{|k,v| ["shipping_#{k}",v]}]
p hsh2
# >> {"shipping_address"=>"foo", "shipping_postcode"=>"bar"}

How to shift the values in a hash to another key


hash1 = { a: [1, 2, 3], b: [4, 5, 6], c: [7, 8, 9] }
keys = hash1.keys
=> [:a, :b, :c]
values = hash1.values
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

keys.zip(values.unshift([])).to_h
=> {:a=>[], :b=>[1, 2, 3], :c=>[4, 5, 6]}

Best way to convert strings to symbols in hash

In Ruby >= 2.5 (docs) you can use:

my_hash.transform_keys(&:to_sym)

Using older Ruby version? Here is a one-liner that will copy the hash into a new one with the keys symbolized:

my_hash = my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

With Rails you can use:

my_hash.symbolize_keys
my_hash.deep_symbolize_keys

Convert hash keys to lowercase -- Ruby Beginner

You can use something like this:

CSV.foreach(file, :headers => true) do |row|
new_hash = {}
row.to_hash.each_pair do |k,v|
new_hash.merge!({k.downcase => v})
end

Users.create!(new_hash)
end

I had no time to test it but, you can take idea of it.

Hope it will help



Related Topics



Leave a reply



Submit