From: Alex Allmont Date: 2011-06-03T23:27:33+09:00 Subject: Best way to replace hash keys What is the most optimal and neatest way to replace keys in a hash given a particular condition? I'm currently using delete_if and putting replacement values into a new hash before merging them back into the original hash. Another approach would to simply rebuild a whole new hash. # Contrived example: given searchReplacePairs hash of { search => replace } pairs, # convert any String keys to a Regexp. newPairs = { } searchReplacePairs.delete_if do |search, replace| if search.class == String newPairs[Regexp.new(search)] = replace end end searchReplacePairs.merge! newPairs Many thanks, Alex