class Hash
def assert_valid_keys(*valid_keys)
{ name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: name"
{ name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
use strings for keys but assert symbols as keys, this will fail.
on a mismatch. Note that keys are NOT treated indifferently, meaning if you
Validate all keys in a hash match *valid_keys, raising ArgumentError
def assert_valid_keys(*valid_keys) valid_keys.flatten! each_key do |k| raise ArgumentError.new("Unknown key: #{k}") unless valid_keys.include?(k) end end