Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created July 24, 2013 16:55
Show Gist options
  • Save timuruski/6072363 to your computer and use it in GitHub Desktop.
Save timuruski/6072363 to your computer and use it in GitHub Desktop.
Trying to tease out STDIN/OUT interactivity for Ruby scripts
puts "STDIN: #{STDIN.tty?}, $stdin: #{$stdin.tty?}"
puts "STDOUT: #{STDOUT.tty?}, $stdout: #{$stdout.tty?}"
#!/usr/bin/env bash
ruby test.rb
@timuruski
Copy link
Author

Try running the Ruby script directly, the shell script and running it through a bash invocation and sub-shell, the value of tty? is always true.

@timuruski
Copy link
Author

$ ruby test.rb
> STDIN: true, $stdin: true
> STDOUT: true, $stdout: true

$ ruby test.rb | cat
> STDIN: true, $stdin: true
> STDOUT: false, $stdout: false

$ echo | ruby test.rb | cat
STDIN: false, $stdin: false
STDOUT: false, $stdout: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment