From: Daniel Sheppard Date: 2005-11-09T09:47:34+09:00 Subject: Re: Better way to build string I'm guessing your code either includes name/value pairs for html/xml tag attributes or for a query string. I don't see why those two things would be put in the same function, but if you REALLY want them to spend their lives together: return "" if properties.empty? if prop_type == "tag" properties.map {|item| " #{item.name}=#{item.value}"} #or alternatively: #" " + properties.map {|item| "#{item.name}=#{item.value}"}.join(" ") else "?" properties.map {|item| "#{item.name}=#{item.value}"} end If you're using this function for what I think you're using it for... make sure you escape things properly. For the query strings, use: require 'uri' "?" + properties.map {|item| "#{item.name}=#{URI.escape(item.value)}"} For the attributes, make sure that you escape the values to remove &," and ' and any other character that doesn't belong in your encoding. Even better, use something like Builder if you're building up XML/XHTML. > -----Original Message----- > From: marcus [mailto:m-lists@bristav.se] > Sent: Wednesday, 9 November 2005 5:41 AM > To: ruby-talk ML > Subject: Better way to build string > > I'm trying to build a query string based on an array of > properties. Is there a shorter/nicer way to do the following? > > def build_query_string(properties, prop_type) > result = "" > > properties.each do |item| > if prop_type == "tag" > result += " #{item.name}=#{item.value}" > else > if result == "" > result = "?" > else > result += "&" > end > result += "#{item.name}=#{item.value}" > end > end > > result > end > > /Marcus > > > > ##################################################################################### This email has been scanned by MailMarshal, an email content filter. #####################################################################################