Skip to content

Instantly share code, notes, and snippets.

@zug-zug
Created July 12, 2018 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zug-zug/2f2e81f4963333542a90ced87595f5bc to your computer and use it in GitHub Desktop.
Save zug-zug/2f2e81f4963333542a90ced87595f5bc to your computer and use it in GitHub Desktop.
Why does output for `Open3.capture2e("ruby", <file>)` and `Open3.capture2e(<file>)` differ?
#!/usr/bin/ruby
=begin
This is a simple executable that just parrots what you type back at you, on stdout:
$ ./parrot.rb
> foo
! foo
> bar
! bar
PUZZLE: Why does output for Open3.capture2e("ruby", "./parrot.rb") and Open3.capture2e("./parrot.rb") differ?
e.g. ./parrot.rb ### only prints the script's stdout
ruby -ropen3 -e 'puts Open3.capture2e("./parrot.rb", stdin_data: "foo\nbar\n").first.inspect'
=> "! foo\n! bar\n"
e.g. ruby ./parrot.rb ### prints both stdin and stdout
ruby -ropen3 -e 'puts Open3.capture2e("ruby", "./parrot.rb", stdin_data: "foo\nbar\n").first.inspect'
=> "> foo\n> bar\n> ! foo\n! bar\n"
=end
require 'readline'
loop do
cmd = Readline.readline("> ", true)
exit if cmd.nil?
puts "! #{cmd}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment