From: "David A. Black" Date: 2009-07-20T20:20:05+09:00 Subject: Re: How to strip inner whitespace in rails Hi -- On Mon, 20 Jul 2009, 7stud -- wrote: > Thriving K. wrote: >> I want to strip any whitespace that more than 1 to be 1 >> >> For example "A B C" ===> "A B C" >> >> "Very Important" ===> "Very Important" > > split() rules the world. squeez()...not so much. > > s.split().join(" ") They're completely different methods, though. They're not in a death struggle :-) They do different things. I wouldn't want to try this with split: "abc deffff ghi".squeeze(" f") => "abc def ghi" squeeze is a better fit, it terms of its stated purpose, than split, if you want to condense multiple occurrences into one. It's also faster, in every benchmark I can come up with: user system total real 2.630000 0.010000 2.640000 ( 2.665775) 0.140000 0.000000 0.140000 ( 0.136925) (That's for removing extra " "s from a string of length about 500.) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) Training! Intro to Ruby, with Black & Kastner, September 14-17 (More info: http://rubyurl.com/vmzN)