What is the most elegant way in Ruby to remove a parameter from a URL?

Maybe a little off-topic, but for anyone who's attempting to do this in the context of a rails app you can simply do:

url_for(params.except(:name_of_param_to_delete))

N.B. Tested in rails v2.3.9.


I prefer to use:

require 'addressable/uri'

uri = Addressable::URI.parse('http://example.com/path?param1=one&param2=2&param3=something3')

params = uri.query_values #=> {"param1"=>"one", "param2"=>"2", "param3"=>"something3"}
params.delete('param1') #=> "one"
uri.query_values = params #=> {"param2"=>"2", "param3"=>"something3"}

uri.to_s #=> "http://example.com/path?param2=2&param3=something3"

Tags:

Ruby

Url

Parsing