From: Francis Hwang Date: 2005-02-14T02:02:16+09:00 Subject: Re: "Joining" strings which may be nil (or) Handling Option hashes On Feb 13, 2005, at 11:44 AM, Robert Klemme wrote: > Lots of, here are some: > >>> opts = {"title"=>"ruby", "author"=>"dave", "publisher"=>"oreilly", >>> "foo"=>nil} > => {"title"=>"ruby", "author"=>"dave", "foo"=>nil, > "publisher"=>"oreilly"} >>> opts.select{|k,v|v} > => [["title", "ruby"], ["author", "dave"], ["publisher", "oreilly"]] >>> opts.select{|k,v|v}.map{|k,v| "#{k}=#{v}"}.join(" and ") > => "title=ruby and author=dave and publisher=oreilly" > > Here's a more efficient variant - using #inject of course :-) > >>> opts.inject(nil){|s,(k,v)| v ? (s ? s << " and " : "") << k << "=" >>> << v : s} > => "title=ruby and author=dave and publisher=oreilly" Maybe the OP isn't worried about this, but: Is there a way to represent ORs as well as ANDs if you're doing this? Francis Hwang http://fhwang.net/