Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #6678
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Updating a Hash Value Based On Same-Key in Another Hash |
| Date | 2012-11-19 21:19 +0100 |
| Message-ID | <agvijfFra3pU1@mid.individual.net> (permalink) |
| References | <05583123-f97b-49ab-ae03-857af2dcb8fc@googlegroups.com> |
On 19.11.2012 16:51, Jeff Nyman wrote:
> Hey all.
>
> I have an issue where I have two hashes. What I'm trying to do is replace a value in the original hash if the second hash has a key matching one of the keys in the first has. To be specific, I have these two hashes:
>
> data = {"study"=>"Lucid Study", "name"=>"Lucid Plan", "studyWillBe"=>"Combination"}
>
> conditions = {"study"=>"((current))"}
>
> What I want to have happen is the data hash should have its "study" key updated since that key is matched in the conditions hash. So I want data to end up like this:
>
> data = {"study"=>"((current))", "name"=>"Lucid Plan", "studyWillBe"=>"Combination"}
>
> I'm having a mental block on this. I got this far:
>
> data = Hash[data.map {|k, v| [conditions[k] || k, v] }]
>
> But that's not quite doing the trick and I'm finding it hard to get the effect I want.
>
> Can anyone point me in the right direction? Much appreciated.
data.keys.each do |k|
conditions.has_key?(k) and data[k] = conditions[k]
end
keys = data.keys
keys.zip(conditions.values_at(*keys)) do |k,v|
data[k] = v unless v.nil?
end
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
Updating a Hash Value Based On Same-Key in Another Hash Jeff Nyman <jeffnyman@gmail.com> - 2012-11-19 07:51 -0800 Re: Updating a Hash Value Based On Same-Key in Another Hash Robert Klemme <shortcutter@googlemail.com> - 2012-11-19 21:19 +0100
csiph-web