Created
February 5, 2014 01:49
-
-
Save baweaver/8816103 to your computer and use it in GitHub Desktop.
retrieve all keys listed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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