From: Ronald Fischer Date: 2007-09-10T22:44:30+09:00 Subject: Re: gsub! and quoting question > > data=%q(') > > puts ":"+data+" replace quotes by "+%q(\\') > > data.gsub!(/'/,%q(\\')) > > puts "data length now #{data.length}" > > > > This produced as output: > > > > :' replace quotes by \' > > data length now 0 > > > > From this I conclude that the gsub! had shortened the > string to length > > zero. > > Any explanation for this? How do I solve my problem in a proper way? > > This comes up frequently. I'm sorry if I posted a question which has been answered already :-( > You need to be aware that there are several > levels of interpretation involved and so you need multiple levels of > escaping. This is sometimes obscured by the fact that '\1' works > although it should read '\\1'. These levels are: 1. string escaping, > 2. regexp replacement string escaping. Hmmm... I would understand your argument if I had a digit following a backslash (which would mean a backreference to the n-th group in the pattern), but here we have \', and this is not a back reference, isn't it? At least the discussion of String#gsub at http://www.ruby-doc.org/core/ mentions only \1,\2,... etc. as back references. Ronald -- Ronald Fischer Phone: +49-89-452133-162