Farhan Salam Farhan Salam - 4 months ago 15
Ruby Question

Replace inner double qoutes in string array - Ruby

I have a string in the form of an array

["foo","bar","foo"bar"baz"]


I am using
eval
method to form a Ruby Array with this string. But of course, the inner double qoutes are not escaped.

[22] pry(main)> eval('["foo","bar","foo"bar"baz"]')
SyntaxError: (eval):1: syntax error, unexpected tIDENTIFIER, expecting ']'
["foo","bar","foo"bar"baz"]
^
(eval):1: syntax error, unexpected ']', expecting end-of-input
["foo","bar","foo"bar"baz"]
^
from (pry):22:in `eval'
[23] pry(main)>


Is there a way I can escape only the ones inside an array element.

Thanks

Answer

I think you can try something like

eval('["foo","bar","foo"bar"baz"]'.gsub(/(\w)\"(\w)/, '\1\"\2'))

if I got what you have meant

Comments