From: David Alan Black Date: 2002-01-12T03:39:43+09:00 Subject: Re: gsub: arrays as parameters Hello -- On Sat, 12 Jan 2002, Jack Dempsey wrote: > I wrote this and it works well for me. I wanted to see what others > thought, and ask if anyone (Matz?) knows the pros and cons of making a > change like this to gsub. It looks more like a new, different method (where the first argument has to respond to each_with_index, which Regexp objects don't), rather than a modification of gsub(!). > > class String > def gsubx!(regex,replace) > regex.each_with_index do |x,i| > self.gsub!(x,replace[i]) > end > end > > def gsubx(regex,replace) > str = self.dup > regex.each_with_index do |x,i| > str.gsub!(x,replace[i]) > end > str > end Couldn't that be: def gsubx(*args) dup.gsubx!(*args) end > end You've eliminated the block form -- i.e., your gsubx(!) doesn't take a block, which gsub(!) does. Also, I'm not exactly sure what this gains you, over using a pattern with '|' in it. I probably haven't puzzled through it enough. Can you give an example? David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav