From: James Britt Date: 2007-01-13T02:04:31+09:00 Subject: Re: DRY gsub... Phrogz wrote: > Josselin wrote: >> I wrote the following ruby statements.. I get the result I need , I >> tried to DRY it for 2 hours without being successfull , >> >> d = d.gsub(/\r\n/,' ') # get rid of carriage return >> d = d.gsub(/;/,' ') # replace column by space >> d = d.gsub(/,/,' ') # replace comma by space >> a = d.split(' ') # split into component , space as divider > > BTW, that is already reasonably DRY, in my opinion. Calling the same > method repeatedly but with different parameters is not "repeating > yourself". 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. -- James Britt http://www.ruby-doc.org - Ruby Help & Documentation http://www.rubystuff.com - The Ruby Store for Ruby Stuff http://www.jamesbritt.com - Playing with Better Toys