Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created February 5, 2014 01:49
Show Gist options
  • Save baweaver/8816103 to your computer and use it in GitHub Desktop.
Save baweaver/8816103 to your computer and use it in GitHub Desktop.
retrieve all keys listed
[1] pry(main)> hash = {a: 1, b: 2, c: 3}
=> {:a=>1, :b=>2, :c=>3}
[2] pry(main)> class Hash
[2] pry(main)* def retrieve_keys(*keys)
[2] pry(main)* self.reduce({}) { |h, (k, v)|
[2] pry(main)* keys.include?(k) ? h.merge!({k => v}) : h
[2] pry(main)* }
[2] pry(main)* end
[2] pry(main)* end
=> nil
[3] pry(main)> hash.retrieve_keys(:a, :b)
=> {:a=>1, :b=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment