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
I won't claim this is the best way but it may be worth a look.
Is it doing what you want or did I misunderstand?
a = {"ONE"=>"1",2=>"2","THREE"=>"3"}
t = a.to_a.transpose
b = Hash[*(t[0].map{|x| x.class==String ? Regexp.new(x) :x}.zip(t[1])).flatten]
p b #> {/ONE/=>"1", 2=>"2", /THREE/=>"3"}
Harry
···
On Fri, Jun 3, 2011 at 11:27 PM, Alex Allmont <Alex.Allmont@naturalmotion.com> wrote:
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
On Jun 3, 10:27 am, Alex Allmont <Alex.Allm...@naturalmotion.com> wrote:
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
I'll probably work with rekey because of the clean syntax, very readable. Plus that extension generally looks very useful.
Thanks for the link Avdi, I've been struggling with using inject so this gives great insight.
I am picking apart Harry's example but I find it hard to read and I wonder if the array conversion, transposing, zipping, and flattening would be inefficient. Always interested in seeing alternative uses though
Best wishes,
Alex
···
-----Original Message-----
From: avdi@inbox.avdi.org [mailto:avdi@inbox.avdi.org] On Behalf Of Avdi Grimm
Sent: 04 June 2011 22:52
To: ruby-talk ML
Subject: Re: Best way to replace hash keys
On Fri, Jun 3, 2011 at 10:27 AM, Alex Allmont <Alex.Allmont@naturalmotion.com> wrote:
What is the most optimal and neatest way to replace keys in a hash given a particular condition?