Ruby EventMachine EM.system在命令中丢失转义反斜杠
我有以下Ruby代码,它使用EM.system启动第二个Ruby脚本:Ruby EventMachine EM.system在命令中丢失转义反斜杠,ruby,eventmachine,yajl,Ruby,Eventmachine,Yajl,我有以下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 en
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()时的输出:
有人知道为什么EM.system会删除逃逸的反斜杠,以及我该如何处理它吗?这似乎对我有效。当我运行它时,我得到
接收的{“foo”:“bar\“hello\”}
。您使用的是什么版本的Ruby/EM/Yajl?你试过更新它们吗?
The cmd is ruby decoder.rb "{\"foo\":\"bar \\\"hello\\\"\"}"
Received {"foo":"bar \"hello\""}