I have the following program:
args = ["a", "b"]
cmd_args = args.map{|x| x.inspect}
str = cmd_args.join(' ')
puts str
The output is:
"a" "b"
I expect the output to be like the following (sub-string quoted with '
instead of "
):
'a' 'b'
I don't want to do a gsub
after string inspect
because, in my real system, substring might contain "
. For example:
args = ['a"c', "b"]
cmd_args = args.map{|x| x.inspect.gsub('"', '\'')}
str = cmd_args.join(' ')
puts str
will output:
'a\'c' 'b'
The "
between a and c is wrongly replaced. My expected output is:
'a"c' 'b'
How can I make string inspect to quote strings with '
instead of "
?
s = 'a"c'.inspect
s[0] = s[-1] = "'"
puts s.gsub("\\\"", "\"") #=> 'a"c'