Skip to content

Instantly share code, notes, and snippets.

@codeslinger
Forked from r38y/hash_filter.rb
Created December 23, 2009 19:24
Show Gist options
  • Save codeslinger/262731 to your computer and use it in GitHub Desktop.
Save codeslinger/262731 to your computer and use it in GitHub Desktop.
require 'yaml'
class Hash
def filter(*keys)
self.inject({}) {|h,(k,v)| h[k] = v if keys.include?(k); h}
end
end
test_hash = {'one' => 1, 'two' => 2, 'three' => 3}
puts test_hash.to_yaml
puts test_hash.filter('one').to_yaml
puts test_hash.filter('one', 'two').to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment