From: James Edward Gray II Date: 2004-09-19T07:04:37+09:00 Subject: Re: Method improvement request .-- On Sep 18, 2004, at 4:29 PM, Charles Hixson wrote: > I'm sure there must be a more idiomatic+efficient way to do this, but > I can't figure it out. Any suggestions? > Also, I'm not sure all of the tests are necessary. Many of them were > added to avoid "Nil class does not implement..." messages. Is there a > better approach? I'll probably get in trouble for this around here, but I came from Perl... > # parse1 separates a chunk into the non-word stuff before it, the word > stuff, and the non-word stuff after it > # word stuff is letters, digits, hyphens, and periods > def parse1(chunk) > pb = /^([^-A-Za-z0-9]*)/ > pe = /([^-A-Za-z0-9]*)$/ > mtch = pb.match(chunk) > a = mtch[0] > mtch = pe.match(mtch.post_match) > b = mtch.pre_match > c = mtch[0] > #print " parse1:a #{a.inspect} " if a and > a.length > 0 > yield a if a and a.length > 0 > #print " parse1:b #{b.inspect} " if b and > b.length > 0 > yield b if b and b.length > 0 > #print " parse1:c #{c.inspect} " if c and > c.length > 0 > yield c if c and c .length > 0 > end What about something like: def parse1(chunk) if chunk =~ / ^([^-A-Za-z0-9]*) # pre-match (.*) # middle ([^-A-Za-z0-9]*)$ # post-match /x a, b, c = $1, $2, $3 end end Does that give you some ideas? James Edward Gray II