Skip to content

Instantly share code, notes, and snippets.

@baxter
Created December 5, 2010 19:20
Show Gist options
  • Save baxter/729370 to your computer and use it in GitHub Desktop.
Save baxter/729370 to your computer and use it in GitHub Desktop.
Code snippets related to building strings in Ruby
parameters.collect do |key, value|
"#{key}=#{value}"
end
# => ["ns0=1", "title=Special:Search", "redirs=1", "advanced=1", "fulltext=Advanced+search", "search=Ruby"]
parameters.collect do |key, value|
"#{key}=#{value}"
end.join "&"
# => "ns0=1&title=Special:Search&redirs=1&advanced=1&fulltext=Advanced+search&search=Ruby"
host = "en.wikipedia.org"
base_uri = "/w/index.php"
parameters = {
"title" => "Special:Search",
"redirs" => "1",
"search" => "Ruby",
"ns0" => "1",
"advanced" => "1",
"fulltext" => "Advanced+search",
}
def querystring(p)
p.collect do |key, value|
"#{key}=#{value}"
end.join "&"
end
querystring(parameters)
# => "ns0=1&title=Special:Search&redirs=1&advanced=1&fulltext=Advanced+search&search=Ruby"
def querystring(p)
query_string = ""
p.each do |key, value|
query_string << "#{key}=#{value}&"
end
query_string
end
querystring(parameters)
# => "ns0=1&title=Special:Search&redirs=1&advanced=1&fulltext=Advanced+search&search=Ruby&"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment