I am using Ruby on Rails 3 and I am trying join
an array with the &
character. I read the Ruby documentation about that.
My array is:
I sometimes have the opposite requirement. I sometimes want to put a certain character after the joined up string. To do that, I usually do
["name1", "name2"].join("&") + "&"
If you're wondering "Why doesn't Ruby implement the ability to add something at the beginning?", my answer is that if was going to handle that kind of scenario, there'd be too many possibilities to consider:
&
in between elements?&
between elements, and before the start?&
between elements, and after the end?&
between elements, and before the start, and after the end?&
between elements, and \n
after the end??
before the elements, &
between the elements, and \n
after the end?As a side note, if you're trying to build up a URL, you might want to use a library rather than do it manually.