Skip to content

Instantly share code, notes, and snippets.

@theHamdiz
Created April 6, 2016 11:48
Show Gist options
  • Select an option

  • Save theHamdiz/af96fb3e953d7c761a5147fcf7b7c078 to your computer and use it in GitHub Desktop.

Select an option

Save theHamdiz/af96fb3e953d7c761a5147fcf7b7c078 to your computer and use it in GitHub Desktop.
Select only the attributes you want from a hash.
# 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