Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are array parameters in rails guaranteed to be in the order in which they appear in the url?

Given the following url: http://example.com?arr[]=hello&arr[]=to&arr[]=you

Am I able to bank on the fact that:

params[:arr] == ['hello', 'to', 'you']

?

I ask because I have some additional data that will be sent with the request that needs to be mapped to each of the values in params[:arr].

like image 244
Brad Avatar asked Sep 29 '10 15:09

Brad


1 Answers

Yes, they are.

Well, maybe a prove from the code where URL parameters are parsed would be handy (I've ommited some code from the example):

#
# file: ../rack-1.2.1/lib/rack/utils.rb
#

def normalize_params(params, name, v = nil)

  # code ommited for simplicity...

  if after == ""
    params[k] = v
  elsif after == "[]"
    params[k] ||= []
    # HERE IT IS!
    params[k] << v
  elsif
  # code ommited for simplicity...
  # ...
end

well, you should take a look yourself but as you can see, the crucial part is where values are simply added to the array - this operation will keep the order.

like image 117
Radek Paviensky Avatar answered Oct 31 '22 23:10

Radek Paviensky