Simply extract named arguments from a hash. Require or allow a set of keys.
When using hashes to accomplish named-argument semantics for methods, we noticed a common pattern:
We often pull options out into local vars at the head of a method
We often raise argument errors for missing keys in the hash
RequireOptions accomplishes both tasks succinctly.
gem install require_options
class Car include RequireOptions def drive(opts) gear, speed = require_options(opts, :speed, :gear) # opts end def put_in_trunk(opts) allow_options opts, :flare, :tire, :luggage # opts may contain any and all of the named keys, but no others end def register(opts) require_at_least_one opts, :license, :ssn # This method wants to use either opts[:license] OR opts[:ssn] end def park(opts) filter_options(opts, :spot) # filter out keys besides :spot, for security purposes, e.g. end end