Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detection of redirection, possible?

0 views
Skip to first unread message

Shannon Fang

unread,
Oct 27, 2005, 2:44:54 AM10/27/05
to
Hi All,

How can I detect a ruby program's stdio has been redirected? I would like to
do this:

puts "show message to user"
$stdin.gets unless $stdin.redirected?

This way, if the program runs normally, it show message and wait for user
interaction, if it is redirected to a file, then just puts to that file
without wating for user intervention.

Thanks!
Shannon


Eric Hodel

unread,
Oct 27, 2005, 3:44:17 AM10/27/05
to

I think you want IO#tty?:

$ ruby -e 'p $stdin.tty?'
true
$ ruby -e 'p $stdin.tty?' < /dev/zero
false

--
Eric Hodel - drb...@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


nobuyoshi nakada

unread,
Oct 27, 2005, 3:49:19 AM10/27/05
to
Hi,

At Thu, 27 Oct 2005 15:44:54 +0900,
Shannon Fang wrote in [ruby-talk:162881]:


> puts "show message to user"

$stdin.gets if $stdin.tty?

--
Nobu Nakada


0 new messages