Skip to content

Instantly share code, notes, and snippets.

@trevrosen
Created September 25, 2013 02:29
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 trevrosen/6694449 to your computer and use it in GitHub Desktop.
Save trevrosen/6694449 to your computer and use it in GitHub Desktop.
Hash#fetch -- a good approach for required arguments in argument hashes
def foobar(args={})
@required = args.fetch(:required) # .fetch will throw exception if key doesn't exist in Hash
@optional = args[:optional] # simple access returns nil if key isn't set -- no exception raised
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment