From: Robert Klemme Date: 2008-08-15T18:18:20+09:00 Subject: Re: Inconsistent regexp behavior 2008/8/15 Peņa, Botp : > From: Glen Holcomb [mailto:damnbigman@gmail.com] > # puts "toads suck".gsub(/(\s)/, "\\#{$1}") > # toads\suck > # Can anyone explain why this is happening. I'm convinced it > # shouldn't ever happen. > > it's a common surprise, but ruby has a solution for that; try the block form > > irb(main):002:0> puts "toads suck".gsub(/(\s)/) { "\\#{$1}" } > toads\ suck > => nil I was thinking about including a statement about the block form but decided against - now you made me write it anyway... :-) IMHO the block form should be restricted to cases where substitutions should be done that cannot be implemented with simple pattern replacement (e.g. inserting a match counter, doing lookups somewhere etc.). This is mainly because I find the non block form more appropriate if you are only doing pattern replacements but it also has the nice side effect of being more efficient. Use the right tool for the job. Kind regards robert -- use.inject do |as, often| as.you_can - without end