Created
September 25, 2013 02:29
-
-
Save trevrosen/6694449 to your computer and use it in GitHub Desktop.
Hash#fetch -- a good approach for required arguments in argument hashes
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
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