我有以下 Ruby 代码,它使用 EM.system 启动第二个 Ruby 脚本:
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
第二个脚本(decoder.rb)这样做:
puts "Received #{ARGV[0]}"
begin
Yajl::Parser.parse(ARGV[0])
rescue => e
puts e
end
输出是:
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) ------^
似乎 EM.system 正在剥离“bar \"hello\"" 中的转义反斜杠。
这是我使用 system() 而不是 EM.system() 时的输出:
The cmd is ruby decoder.rb "{\"foo\":\"bar \\\"hello\\\"\"}"
Received {"foo":"bar \"hello\""}
任何人都知道为什么 EM.system 会删除转义的反斜杠,以及我如何绕过它?