Skip to content

Instantly share code, notes, and snippets.

@sergeych
Created March 30, 2011 05:12
Show Gist options
  • Save sergeych/893895 to your computer and use it in GitHub Desktop.
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
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