Skip to content

Instantly share code, notes, and snippets.

@lulalala
Created August 14, 2012 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lulalala/3347072 to your computer and use it in GitHub Desktop.
Save lulalala/3347072 to your computer and use it in GitHub Desktop.
How to append a parameter into the url in Ruby?
u = URI.parse("http://example.com/user/1234?a=1&b=2")
# <URI::HTTP:0x007fe3d9a1f238 URL:http://example.com/user/1234?a=1&b=2>
query = u.query #"a=1&b=2"
query = query << "&c=3"
u.path #"/user/1234"
u.to_s #"http://example.com/user/1234?a=1&b=2"
u.merge(query).to_s #"http://example.com/user/a=1&b=2&c=3" WHY DOES 1234 disappear?!
# I want to get http://example.com/user/1234?a=1&b=2&c=3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment