Tuesday, November 17, 2015

Maintaining keyword arity is hard

You know, sometimes you just can't bother with explicitly stating what keyword arguments that you want to pass.

def address_method(city: "Bloomington", state: "IN", street: "123 fake st")
print x,y,z,"\n"
end
input = {city: "Indianapolis", street: "321 faux st", zip: "12345"}
address_method(input_address) # => ArgumentError: unknown keyword: zip
addr_mthd = method(:address_method)
acceptable_keywords = addr_mthd.parameters.select{|p| p[0] == :key}.map(&:last)
accepted_input = input.select{|(k,v)| acceptable_keywords.include?(k)}
address_method(accepted_input) # => IndianapolisIN321 faux st