Created
March 30, 2011 05:12
-
-
Save sergeych/893895 to your computer and use it in GitHub Desktop.
Extract Hash params from the array if the last element is a Hash
This file contains 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
class Array | |
## Extract Hash params from the array with defaults if the last element is | |
# a Hash (otherwise returns defaults as is). To be used with *args, e.g. | |
# | |
# def function *args | |
# params = args.extract_params! { :defval => 'foobar' } | |
# ... | |
# end | |
def extract_params! defaults = {} | |
if (dict=last).is_a?(Hash) | |
defaults.update dict | |
pop | |
end | |
defaults | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment