Skip to content

Instantly share code, notes, and snippets.

@thoolihan
Created March 20, 2014 20:57
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 thoolihan/9673643 to your computer and use it in GitHub Desktop.
Save thoolihan/9673643 to your computer and use it in GitHub Desktop.
h1 = {a: 1, b: 2}
h2 = h1.keep_if{|k,v| k == :a}
p h1.inspect # => {a: 1}
p h2.inspect # => {a: 1}
@thoolihan
Copy link
Author

was thinking this was a bug. however, looking at the documentation for this method, it appears that it is intentional for this to delete keys in the hash of the original hash. Seems like by convention, it should have a "!" at the end of the method name though...

@thoolihan
Copy link
Author

if that's not the desired outcome, easiest fix seems to be:

h2 = h1.clone.keep_if{|k,v| k == :a}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment