1

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?

pje
  • 21,801
  • 10
  • 54
  • 70
Jennifer Hickey
  • 632
  • 1
  • 4
  • 9
  • This seems to work for me. When I run it, I get `Received {"foo":"bar \"hello\""}`. What version Ruby/EM/Yajl are you using? Have you tried updating them? – pje Oct 16 '12 at 07:43

0 Answers0