Given the following script, I see another output using Ruby 1.8.7 and Ruby 1.9.2. My question is, what has changed in the ruby hashes that provide this particular behavior?
def to_params(_hash)
params = ''
stack = []
_hash.each do |k, v|
if v.is_a?(Hash)
stack << [k,v]
else
params << "#{k}=#{v}&"
end
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
params << "#{parent}[#{k}]=#{v}&"
end
end
end
params.chop!
params
end
q = {"some_key"=>["some_val"], "another_key"=>["another_val"]}
n = convert_params(q)
puts n
some_key=some_val&another_key=another_val
some_key=["some_val"]&another_key=["another_val"]
1.9.2 stores the type of "Array" value, while 1.8.7 implicitly changes the type to a string.
source
share