Farhan Salam Farhan Salam - 4 months ago 9
Ruby Question

Replace inner double quotes in a string in an array

I have strings in an array:

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


I am using
eval
method to form an array with this string, but the inner double quotes are not escaped:

eval('["foo","bar","foo"bar"baz"]') # => SyntaxError


Is there a way to escape only the ones inside an array element?

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