Created
April 8, 2015 09:11
-
-
Save wteuber/ba0ec8e7220c45919f76 to your computer and use it in GitHub Desktop.
slice ruby Hash
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
params = Hash[[*'a'..'j'].zip([*1..10])] # => {"a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5, "f"=>6, "g"=>7, "h"=>8, "i"=>9, "j"=>10} | |
slice_keys = ['a','f','g'] | |
filter = -> (key, _) { slice_keys.include?(key) } | |
slice = params.select &filter | |
params.delete_if &filter | |
slice # => {"a"=>1, "f"=>6, "g"=>7} | |
params # => {"b"=>2, "c"=>3, "d"=>4, "e"=>5, "h"=>8, "i"=>9, "j"=>10} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment