Skip to content

Instantly share code, notes, and snippets.

@wteuber
Created April 8, 2015 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wteuber/ba0ec8e7220c45919f76 to your computer and use it in GitHub Desktop.
Save wteuber/ba0ec8e7220c45919f76 to your computer and use it in GitHub Desktop.
slice ruby Hash
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