Skip to content

Instantly share code, notes, and snippets.

@r38y
Created December 23, 2009 19:17
Show Gist options
  • Save r38y/262723 to your computer and use it in GitHub Desktop.
Save r38y/262723 to your computer and use it in GitHub Desktop.
require 'yaml'
class Hash
def filter(*keys)
self.inject({}){|h,v| h[v.first] = v.last if keys.include?(v.first); 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