From: Paul Rubel Date: 2004-11-04T05:03:02+09:00 Subject: Re: escaping single quotes in a string with gsub David, David A. Black writes: > Hi -- > > On Thu, 4 Nov 2004, Paul Rubel wrote: > I admit I get confused by escaping and stuff... but I can't quite > picture the case you're describing. If a string contains a single > quote: > > "abc'def" > > that's the same as: > > "abc\'def" > > So I don't think you'll actually see that backslash before the single > quote when you scan the string. Insightful. I suspect you're right and that I made things needlessly complicated (at least at this point). When my code sees the string the escaping should already have occurred. > > If you do see a slash -- i.e., if the string is: > > abc\'def > > then that would probably be generated with "abc\\'def", which would be > equivalent to: > > "abc\\\'def" > > I'm afraid I didn't quite follow the Summer's Day example. Can you > give another? The context that I saw the problem was the following: The code takes in a name and a value and then evals them. If the var_value has an unescaped single quote it would give an error that the string was malformed. to_eval = "#{var_name} = '#{var_value}'"; eval(to_eval, binding) Looking at it now I expect that a backslash in the var_value will cause problems most of the time as the strings contents get interpolated a second time during the eval. Is there a better way to set a value in a binding? The implementation has the option to set values in a hash rather than in the binding but I'd like to keep both if possible. thank you, Paul