Skip to content

Instantly share code, notes, and snippets.

@morhekil
Created June 23, 2015 23:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save morhekil/89a27493811e02b3bf1b to your computer and use it in GitHub Desktop.
Save morhekil/89a27493811e02b3bf1b to your computer and use it in GitHub Desktop.
Destructuring Ruby hashes with named parameters and anonymous splat
def stuff
yield action: 'Boom', schedule: '7PM', color: :red, debug: true
end
# When a hash is yielded to a block, we can used named arguments to
# capture the keys we want, and an anonymous splat to ignore
# everything else
stuff { |action:, schedule:, **| p "Doing #{action} at #{schedule}" }
#
# > ruby ./anonsplat.rb
# Doing Boom at 7PM
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment