Created
November 22, 2009 16:30
-
-
Save ajsharp/240626 to your computer and use it in GitHub Desktop.
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
# Method to provide some syntactic sugar to use a hash of options instead of normal | |
# arguments, where you want an ArgumentError thrown if the proper keys aren't passed | |
# in, but you want the syntactic sugar of hash keys. | |
# | |
# Example | |
# ======= | |
# require_options!({ :name => "John Doe", :age => 24 }, :name, :age, :sex) | |
# # => raises an ArgumentError | |
# | |
def require_options!(opts, *required_options) | |
required_options.each do |required_option| | |
raise(ArgumentError, "wrong number of arguments (#{opts.keys.size} for #{required_options.size})") unless opts.keys.include? required_option | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment