From: Christian Neukirchen Date: 2005-10-04T04:40:30+09:00 Subject: Re: String#scan_and_map (was: Re: split without lookbehind) "Phrogz" writes: >> Following is my hackish attempt to make a String#scan_and_map >> function that does the above. > > OK, that was dumb. On the way to work I realized that the above can far > more correctly be implemented simply as: > > class String > def scan_and_map( regexp ) > results = [] > scan( regexp ){ |args| > results << yield( args ) > } > results > end > end > > str = 'foo,bar ,, baz,qux,,,jorb,jing,,,,blat' > p str.scan_and_map( /(.+?[^,],{2}*)(?:,(?!,)|$)/ ){ |saved,others| > saved > } p str.scan( /(.+?[^,],{2}*)(?:,(?!,)|$)/ ).map{ |saved,others| saved } Trivialized? -- Christian Neukirchen http://chneukirchen.org