From: RichardOnRails Date: 2009-11-13T15:25:04+09:00 Subject: Re: DRYing a Regex On Nov 12, 6:50 pm, James Edward Gray II wrote: > On Nov 12, 2009, at 5:00 PM, RichardOnRails wrote: > > > > > I've got a routine that works fine at building an array of upper-case > > strings extracted from a string: > > > aNewList = [] > > s = StringScanner.new sNewList > > upper = /[A-Z]+/ > > not_upper= Regexp.new(  upper.source.sub( /\[/, '[^' )  ) > > while not s.eos? > >  case > >     when s.skip(upper); aNewList << s.matched > >     else s.skip(not_upper) > >  end > > end > > > But the not_upper Regexp definition is really a kludge.  It somewhat > > camouflages what is really /[^A-Z]+/ > > > I'd like to DRY it by expressing it as something like !upper.  I need > > something like !~ we use normally with string searches. > > > Any ideas? > > Well, you don't really need a StringScanner for this simple task.  Your code really just rebuilds String#scan(): > >   a_new_list = s_new_list.scan(/[A-Z]+/) > > Note that I've also switched your variable naming style to the snake_case that we Rubyists prefer. > > Hope that helps. > > James Edward Gray II Hi James, As I said to Caleb, cutting my 10-liner down to 1 is extreme DRYing!! Thanks for that. As far as underscoring vs. Camel-case goes, I know Rubyists' preference, but I bow to Shakespeare's notion that "a rose by any other name is just as sweet." I spent a couple decades writing/ maintaining Window's application for clients using C and C++, so I've a fondness for Polish notation (at least that's what I think it was called.) Typing extra hyphens vs pressing the shift key lets me write code faster, and the a/s/h prefix for arrays/strings/hashes helps me avoid a lot of interpreter complaints. And fellow programmers of almost any stripe knows what I mean. Finally, I retired curmudgeon, and you know how we old folks are :-) Seriously, your insight was very helpful and will help me avoid a bunch of wasteful code. Best wishes, Richard