Farhan Salam Farhan Salam - 1 year ago 60
Ruby Question

Replace inner double qoutes in string array - Ruby

I have a string in the form of an array


I am using
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 ']'
(eval):1: syntax error, unexpected ']', expecting end-of-input
from (pry):22:in `eval'
[23] pry(main)>

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


Answer Source

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