Changing every value in a hash in Ruby Changing every value in a hash in Ruby ruby ruby

Changing every value in a hash in Ruby


In Ruby 2.1 and higher you can do

{ a: 'a', b: 'b' }.map { |k, str| [k, "%#{str}%"] }.to_h


If you want the actual strings themselves to mutate in place (possibly and desirably affecting other references to the same string objects):

# Two ways to achieve the same result (any Ruby version)my_hash.each{ |_,str| str.gsub! /^|$/, '%' }my_hash.each{ |_,str| str.replace "%#{str}%" }

If you want the hash to change in place, but you don't want to affect the strings (you want it to get new strings):

# Two ways to achieve the same result (any Ruby version)my_hash.each{ |key,str| my_hash[key] = "%#{str}%" }my_hash.inject(my_hash){ |h,(k,str)| h[k]="%#{str}%"; h }

If you want a new hash:

# Ruby 1.8.6+new_hash = Hash[*my_hash.map{|k,str| [k,"%#{str}%"] }.flatten]# Ruby 1.8.7+new_hash = Hash[my_hash.map{|k,str| [k,"%#{str}%"] } ]


Ruby 2.4 introduced the method Hash#transform_values!, which you could use.

{ :a=>'a' , :b=>'b' }.transform_values! { |v| "%#{v}%" }# => {:a=>"%a%", :b=>"%b%"}