3

I'm trying to launch a ruby instance as subprocess of my node program. In fact, everything is OK but I just can't interact with the ruby's STDIN and STDOUT. (of course the ruby program works in my terminal with my keyboard input)

So this is a simplified code that I want to get working ...

simpleproc.js

var util   = require('util'),
    spawn = require('child_process').spawn,
    ruby  = spawn('ruby', [__dirname + '/process.rb']);

ruby.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ruby.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ruby.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});

ruby.stdin.write("ping\n");

process.rb

f = File.new("process.log", "w")
f.write "=== Hello! ===\n"
STDIN.each_line do |line|
   STDOUT.write line
   f.write line
end

What's wrong with it ? I've already managed to get an another process working... but here, there is no IO ! Nothing happens !

EDIT: I modified the ruby file to show that, with node, the file is only written with === Hello! ===\n inside. So we can say that, the ruby file is correctly launched but doesn't receive anything from node (I've tried to flush after the STDOUT.write but the do statement is never executed.

1

1 Answer 1

3

Try STDOUT.flush on the ruby side after STDOUT.write, as the output is being buffered.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but it didn't work because the ruby STDIN, doesn't receive anything. If I try to write in a file in the do statement, nothing happens anyway. So, I can't see where the problem is. But that's maybe due to my node's version (master branch in the github repo).
Maybe it's the version. Using node 0.4.2 and ruby 1.8.7 (Mac) your code works for me after I add a flush (silence without it). Writing to a file or to node from ruby should not make a difference.
Oh boy ! In fact I wrote this ruby.stdin.write("ping"); in my own JS file. But thank you anyway, your answer was good because without the flush instruction, there was no way to get it working ! Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.