Created
December 5, 2010 19:20
-
-
Save baxter/729370 to your computer and use it in GitHub Desktop.
Code snippets related to building strings in Ruby
This file contains 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
parameters.collect do |key, value| | |
"#{key}=#{value}" | |
end | |
# => ["ns0=1", "title=Special:Search", "redirs=1", "advanced=1", "fulltext=Advanced+search", "search=Ruby"] |
This file contains 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
parameters.collect do |key, value| | |
"#{key}=#{value}" | |
end.join "&" | |
# => "ns0=1&title=Special:Search&redirs=1&advanced=1&fulltext=Advanced+search&search=Ruby" |
This file contains 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
host = "en.wikipedia.org" | |
base_uri = "/w/index.php" | |
parameters = { | |
"title" => "Special:Search", | |
"redirs" => "1", | |
"search" => "Ruby", | |
"ns0" => "1", | |
"advanced" => "1", | |
"fulltext" => "Advanced+search", | |
} |
This file contains 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
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" |
This file contains 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
def querystring(p) | |
query_string = "" | |
p.each do |key, value| | |
query_string << "#{key}=#{value}&" | |
end | |
query_string | |
end |
This file contains 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
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