Created
February 10, 2012 10:09
-
-
Save pedromg/1788371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = "param1=1¶m2=2&offset=3" | |
unless query.empty? | |
# build an Hash based on the query string | |
# we want to build {"param1"=>"1", "param2"=>"2", "offset"=>"3"} | |
temp_params = {} | |
query.split("&").each {|v| temp_params.merge!(Hash[*v.split("=")]) unless (v.split("=").count < 2)} | |
# remove the offset parameter | |
temp_params.delete("offset") | |
# convert the Hash back to a String | |
new_query = temp_params.map{|k,v| "#{k}=#{v}"}.join("&") | |
end |
Boas, não estarás a simplificar demais o que pretendes ? ;-)
Não refiro Rails em lado algum, atenção. Estou a referir somente um URI query String, em Ruby.
É parte de código para um paginador genérico.
Hum, tens razão, parti do principio que estarias a usar o Rails.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Boas, não estarás a complicar demais o que pretendes?
Os parametros que recebes vêm em forma de hash.
params = {"param1"=>"1", "param2"=>"2", "offset"=>"3"}
params.delete('offset')
params
resultado => {"param1"=>"1", "param2"=>"2"}