From: Gregory Seidman Date: 2007-01-15T02:13:34+09:00 Subject: Re: DRY gsub... On Sat, Jan 13, 2007 at 09:51:47AM +0900, James Britt wrote: > Gregory Seidman wrote: > >Cleaned up: > > The whole point was *not* to clean it up, but to make obvious what and > why something was happening in the code. > > Brevity is the soul of wit, but it can play havoc with code maintenance. > > >DELIMITERS = Regexp.new([ > > " ", > > "\r\n", > > ";", > > "," > >].map{ |c| Regexp.escape(c) }.join("|")) > > > >a = d.split(DELIMITERS) > > Unless these chunks of code are right next to each other, it may be hard > to know the purpose for the delimiters or what's driving the split. The cleaned up version includes the delimiters in an array of individual strings. Your original complaint was about readability and code maintenance. While I agree that a long literal Regexp can be hard to read and hard to maintain, you can achieve the same efficiency of that Regexp without sacrificing readability using the solution above. Perhaps the following would make you happier? module Whatever DELIMITERS = [ " ", "\r\n", ";", "," ] def split_string(str) @delimiter_regexp ||= Regexp.new(DELIMITERS.map{ |c| Regexp.escape(c) }.join("|")) str.split(@delimiter_regexp) end extend self end a = Whatever.split_string(d) (If you want to make it even fancier so you can modify DELIMITERS at runtime you'll have to do something clever with hashes.) If the code above does not fulfill what you were intending, please do explain why; if I've missed the point, I'd like to know it and to try again at understanding. > James Britt --Greg