Skip to content

Instantly share code, notes, and snippets.

@jpoz
Last active December 13, 2015 21:18
Show Gist options
  • Save jpoz/4975829 to your computer and use it in GitHub Desktop.
Save jpoz/4975829 to your computer and use it in GitHub Desktop.
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