From: YANAGAWA Kazuhisa Date: 2004-09-19T13:02:16+09:00 Subject: Re: Method improvement request .-- In Message-Id: <414CA862.7090402@earthlink.net> Charles Hixson writes: > # parse1 separates a chunk into the non-word stuff before it, the word > stuff, and the non-word stuff after it String#split includes a captured portion in a result array, so you can do: irb(main):008:0> "!@#456&*(".split(/([\w\-.]+)/, 3) => ["!@#", "456", "&*("] irb(main):009:0> "456&*(".split(/([\w\-.]+)/, 3) => ["", "456", "&*("] irb(main):010:0> "!@#456".split(/([\w\-.]+)/, 3) => ["!@#", "456", ""] If a string has extra word-nonword pairs, you should consider that. > # parse2 takes a hunk of word stuff, and possibly separates it at a (snip) > # parse3 takes a hunk of word stuff, and possibly separates it at an So you just do: def parse_chunk(chunk, regexp) chunk.split(regexp, 3).each {|v| yield(v)} end def parse2(chunk, &block) parse_chunk(chunk, /(\.\.\.)/, &block) end def parse3(chunk, &block) parse_chunk(chunk, /(--)/, &block) end -- kjana@dm4lab.to September 19, 2004 Every body's business is nobody's business.