0

From what I understand, this should work, but no luck for me so far. Posting this question hoping I'm doing something really silly. I have:

hello.rb

#!/usr/bin/env ruby

3.times do
  puts 'hello'
  sleep 1
end

tail.js

var cp = require('child_process');
var tail = cp.spawn('./hello.rb');

tail.stdout.pipe(process.stdout);

/* Also tried this: */
tail.stdout.on('data', function(data) {
  console.log(data.toString())
});

hello.rb is executable.

When I run node tail.js, 3 hellos are printed to stdout, but after, the loop completes. You can verify this by changing 3.times to just loop for an infinite loop and seeing nothing in stdout. The infinite loop case is actually what I'm attempting to figure out how to do.

What am I doing wrong?

1 Answer 1

1

You should try either adding $stdout.flush after your puts or by setting $stdout.sync = true at the beginning of your ruby script.

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

3 Comments

That worked, than you! Could you add a little bit about what that does?
I also edited the question title/tags to reflect that this had more to do with ruby than child_process or node.
$stdout.flush flushes the current stdout buffer since writing to stdout is asynchronous by default. $stdout.sync = true switches to synchronous stdout output, which disables the stdout buffer and ensures that each write is flushed before continuing execution.

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.