Skip to content

Instantly share code, notes, and snippets.

@bogn83
Forked from morhekil/anonsplat.rb
Created December 14, 2021 12:22
Show Gist options
  • Save bogn83/3a1e508a57e5828c705a60a9b25d71a3 to your computer and use it in GitHub Desktop.
Save bogn83/3a1e508a57e5828c705a60a9b25d71a3 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