I have a URL in a string. What is the most concise way to add some parameters to it?
e.g.
base = 'http://example.com'
uri1 = some_magical_method(base, :p1 => 'v1') # => http://example.com/?p1=v1
uri2 = some_magical_method(uri1, :p2 => 'v2') # => http://example.com/?p1=v1&p2=v2
uri3 = some_magical_method(uri2, :p3 => nil) # => http://example.com/?p1=v1&p2=v2
1) In Ruby?
2) In Rails?
In Rails, where ActiveSupport is available, the easiest way is to construct a hash, then to call
#to_queryon it.In plain Ruby, the same applies, but you’ll need to write the encoder yourself: