From: Mike McKinney Date: 2007-12-15T04:11:30+09:00 Subject: Re: simple way to turn "foo and bar" to "+foo +bar" ------=_Part_3339_10923467.1197659489338 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline just pass in the delimeter... test.rb: and_str = "foo and bar AND baz" pipe_str = "foo|bar| baz" def translate(str, del) str.split(/\s*#{del}\s*/i).map{|s| "+#{s}"}.join(' ') end puts translate(and_str, 'and') puts translate(pipe_str, '\|') On Dec 14, 2007 1:50 PM, Phrogz wrote: > On Dec 14, 11:22 am, MenTaLguY wrote: > > On Sat, 15 Dec 2007 03:19:22 +0900, Rolando Abarca > wrote: > > >> Can anyone help with an elegant solution? > > > > > "foo and bar and baz".split(/\s+and\s+/i).map{|s| "+#{s}"}.join(' ') > > > > I think this one is best. > > It's elegant, but it assumes that the search phrase always has "and". > Although not originally specified either way, that seems like an > unlikely assumption. But then, I don't know the problem domain. > > ------=_Part_3339_10923467.1197659489338--