I don't see any way to refactor this, but something tells me I'm missing something.
if val.instance_of?(Hash) then
@fields.has_key?(key) ? @fields[key].merge!(val) : @fields.merge!({key => val})
elsif val.instance_of?(Array) then
@fields.has_key?(key) ? @fields[key].push(val) : @fields.merge!({key => Array.new.push(val)})
else
@fields.has_key?(key) ? @fields[key] << val : @fields.merge!({key => val})
end
The point of the method is to take a hash and insert a value of different types if the key doesn't exist:
{:key => "value}, {:key = ["value"]}, or {:key => {:k => "value"}}
or append if it does:
{:key=> "value1value2"}, {:key => [["value1"], ["value2"]], {:key => {:k1 => "value1", :k2 => "value2"}}
it does work as intended, so my issue is thinking that there is likely an easier way to do it.
push
and<<
do the same thing on arrays, but you seem to use them as if they were different), but of course I can't tell for sure unless you specify what you want. \$\endgroup\$