Created
April 6, 2016 11:48
-
-
Save theHamdiz/af96fb3e953d7c761a5147fcf7b7c078 to your computer and use it in GitHub Desktop.
Select only the attributes you want from a hash.
This file contains hidden or 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
| # Select only the attributes you want from a hash. | |
| # this is crazy useful when getting web parameters for example. | |
| # --------------------------------------------------------------------- | |
| require 'active_support/core_ext' | |
| bacon = { delicious: true, color: "red" } | |
| bacon.slice(:color) | |
| # => { color:"red" } | |
| # it returns a new hash, you can slice unknown keys and it still works. | |
| # there's a lot of great stuff in active_support (from the rails gem) | |
| # it mostly stands by itself but it's big so you can require just 'active_support/core_ext' | |
| # to be more precise. if you want everything in active_support do require 'active_support/all' | |
| # but your global namespace might be too crowded. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment