Skip to content

Instantly share code, notes, and snippets.

@ajsharp
Created November 22, 2009 16:30
Show Gist options
  • Save ajsharp/240626 to your computer and use it in GitHub Desktop.
Save ajsharp/240626 to your computer and use it in GitHub Desktop.
# 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