Getting the intersection and lowest values of a hash.
def hash_intersect_min(*hashes)
hashes.inject(hashes.pop.dup) { |i,h| i.delete_if { |k,v| hv=h[k]; next true unless hv; i[k]=hv if hv<v; false }; i }
end
So the following:
hash_intersect_min({:a => 1, :d => 2, :k => 2, :j => 6}, {:k =>1, :a => 4, :j => 4}, {:e => 4, :k => 2, :d => 4, :j => 9})
Produces:
{:k=>1, :j=>4}
I’m completely aware of the extreme triviality this introduces to most programming, but ya know what… sometimes you need quirky crap to get you through the day.
Happy coding!
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.