This post originated from an RSS feed registered with Ruby Buzz
by Matthew Bass.
Original Post: Selecting matching key/value pairs from a hash
Feed Title: Pelargir
Feed URL: http://feeds.feedburner.com/pelargir/
Feed Description: Musings on software and life from Matthew Bass. Regular posts on new web products, tips and tricks, etc.
I ran into a situation today where I needed to pull out all key/value pairs from a hash that matched the keys in a pre-existing array. This is what I initially came up with:
hash = { :foo => "foo", :bar => "bar", :bat => "bat" }
hash.symbolize_keys.reject { |k, v| ![:foo, :bar].include?(k) }
>>> returns { :foo [...]