Last active
December 13, 2015 21:18
-
-
Save jpoz/4975829 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
class Hash | |
def to_url_params | |
elements = [] | |
self.each_pair do |key, value| | |
elements << param_for(key, value).flatten | |
end | |
elements.join('&') | |
end | |
private | |
def param_for(key, value, parent = nil) | |
if value.is_a?(Hash) | |
temp = [] | |
value.each_pair do |key2, value2| | |
temp << param_for(key2, value2, parent ? parent + "[#{key}]" : key.to_s) | |
end | |
return temp | |
else | |
return ["#{parent ? parent + "[#{key}]" : key.to_s}=#{value}"] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment