R
Ronald Fischer
I thought this would be a trivial task, but it seems to be more
difficult than expected:
I have a variable, data, containing some string. I would like to
preprend every
single apostrophe in this string by a backslash. Here is my solution:
data.gsub!(/'/,%q(\\'))
Strangely, this does not work. I tested it with data containing a string
consisting
of a single quote solely, and surrounded the code by "puts" like this:
data=3D%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?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
difficult than expected:
I have a variable, data, containing some string. I would like to
preprend every
single apostrophe in this string by a backslash. Here is my solution:
data.gsub!(/'/,%q(\\'))
Strangely, this does not work. I tested it with data containing a string
consisting
of a single quote solely, and surrounded the code by "puts" like this:
data=3D%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?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162