Ruby convert string to keyword argument -
i have function in ruby takes keyword arguments in following form:
def get_customer_information(name: nil, dob: nil, age: nil, state: nil) ... query db based on supplied parameters end
the user can specify 1 of keyword arguments. there command line interface user can leverage function calling command line call , passing in key/value of information of following form:
>getcustomerinformation name=myname, dob=mydob
the parameters parsed , separated keys , values. however, need know keyword argument associated supplied input command line. there way convert string keyword argument (for example, convert name name:) need not perform string comparison on each parameter determine keyword argument associated with, resulting in call of form:
get_customer_information(name.to_keyword myname, dob.to_keyword mydob)
or not possible in ruby?
you can use .to_sym method. ruby function method convert string symbol.
or using rails own, "symbol name".parameterize.underscore.to_sym. convert string :symbol_name. uses rails naming convention.
Comments
Post a Comment