I have the following Ruby code, which uses EM.system to kick off a second Ruby script:
json = Yajl::Encoder.encode(Yajl::Encoder.encode({:foo=>"bar \"hello\""}))
cmd = "ruby decoder.rb #{json}"
puts "The cmd is #{cmd}"
EM.run do
EM.system(cmd) do |output, status|
puts output
EM.stop
end
end
The second script (decoder.rb) does this:
puts "Received #{ARGV[0]}"
begin
Yajl::Parser.parse(ARGV[0])
rescue => e
puts e
end
The output is:
The cmd is ruby decoder.rb "{\"foo\":\"bar \\\"hello\\\"\"}"
Received {"foo":"bar "hello""}
lexical error: invalid char in json text.
{"foo":"bar "hello""}
(right here) ------^
It seems like EM.system is stripping the escaped backslashes in "bar \"hello\"".
Here is the output if I use system() instead of EM.system():
The cmd is ruby decoder.rb "{\"foo\":\"bar \\\"hello\\\"\"}"
Received {"foo":"bar \"hello\""}
Anyone know why EM.system would remove the escaped backslashes, and how I might get around it?