From: Gregory Seidman Date: 2007-01-13T08:27:56+09:00 Subject: Re: DRY gsub... On Sat, Jan 13, 2007 at 02:04:31AM +0900, James Britt wrote: [...] > Looking at this, and some of the suggested alternatives, I can see how > it would get tedious to add more characters to the "replace with space" > set. > > The use of compact regular expressions doesn't make the code easier to > read or maintain. > > It may be useful to define the set of special characters, then use that > to drive a string transformation. > > REPLACE_WITH_SPACE = %w{ > \r\n > ; > , > }.map{ |c| Regexp.new(c) } > > class String > def swap_to_spaces > s = self.dupe > REPLACE_WITH_SPACE.each do |re| > s.gsub!( re, ' ') > end > s > end > end > > > a = d.swap_to_spaces.split( ' ' ) > > > > Or something along those lines. Cleaned up: DELIMITERS = Regexp.new([ " ", "\r\n", ";", "," ].map{ |c| Regexp.escape(c) }.join("|")) a = d.split(DELIMITERS) > James Britt --Greg