ruby on rails - Pass symbol as Arguments -
i have defined , working method as,
render_format(doc,host,table_info)
i had method called @ location wherein passed arguments as,
render_format("daily transactions in pos", {:doc => xml,:schema_name => "#{schema_name}",:store_code => "#{store_code}"}, :sales_log => "#{sales_log}")
this worked fine.
now have call method as,
render_format(:doc => xml,:host => "bhindi.rw:3000",:table_info => {'hdr' => 'pos_invoices', 'line' => 'pos_invoice_lines', 'id' => 'pos_invoice_id', 'check_image_flag' => 'y'})
but gives argumenterror, wrong number of arguments(1 3) treating whole of 1 single argument. why that?
when use hash last (or only) method argument in list ruby allows omit curly braces. in first call, arguments of different types (strings , hashes), ruby understands these several parameters. in second call, each of parameters hash 1 key-value pair, because of optional outside curly braces ruby interprets 1 hash, giving argumenterror.
you can wrap each hash in own curly braces letting ruby know in fact individual separate hashes:
render_format({ :doc => xml }, { :host => "bhindi.rw:3000" }, { :table_info => {'hdr' => 'pos_invoices', 'line' => 'pos_invoice_lines', 'id' => 'pos_invoice_id', 'check_image_flag' => 'y'} })
you can see in action in first method call - second argument hash wrapped in own curly braces, while last 1 not. if omitted outer curly braces on second argument, ruby interpret second , third arguments 1 hash , give argumenterror, wrong number of arguments(2 3)
error.
alternatively, if can change implementation of method in question, can accept 1 hash argument , separate values key inside method.
Comments
Post a Comment