From: Ibraheem Umaru-Mohammed Date: 2002-10-19T00:50:50+09:00 Subject: Re: gsub and Backslashes * nico [2002-10-17 00:19]: > On Wed, 16 Oct 2002 23:03:10 +0900 > William Djaja Tjokroaminata wrote: > > > Hi Christian, > > > > Can you give an example of how it may be tricky (causing > > subtle/unnoticable bug) for a newcomer? > > > > Regards, > > > > Bill > > I'm assuming he was referring to Botp's wish list not the ruby newcomers list (my mail reader thinks he was replying to that post). > ibz@ignoramus:$ irb Reading config file... done. ruby-1.7.3 on i686-pc-linux with irb 0.9(02/07/03) Type 'help' for more information. >>> str = 'a\b\c' ... >>> puts str a\b\c nil >>> puts str.gsub(/\\/,'\\\\') a\b\c nil >>> puts str.gsub(/\\/,'\\\\\\') a\\b\\c nil >>> puts str.gsub(/\\/,'\\\\\\\\') a\\b\\c nil >>> puts str.gsub(/\\/) { '\\\\' } a\\b\\c nil >>> puts str.gsub(/\\/, '\&\&') a\\b\\c nil >>> ,---- [ Programming Ruby, p.64 ] | When the substitution occurs, the regular expression | engine performs another pass through the string, converting | "\\" to "\", so the net effect is to replace each single | backslash with another single backslash. `---- Kindest regards, --ibz.