IO#dup

Here is my use case, on UNIX. I wrote a ncurses program which can get its data from stdin (similar to what happen to less when run as 'cat file | less'). The problem is that stdin is not connected to your terminal any more and ncurses goes wild interpreting the data from a file as key strokes.
The trick seems, as guessed with lsof on less, to dup stdin and dup2 stdout to stdin. Then ncurses is happy with stdin connected to a terminal, and the data can be read from some higher file descriptor (most probably 4).

How to do the same thing in Ruby? There is no IO#dup or IO#dup2. Only IO#reopen. But I need to already have an object for the destination IO, which I don't have when I do the first "dup".

Right now, I solved the problem by doing the following:

r, w = IO.pipe
w.close
r.reopen($stdin)
$stdin.reopen($stdout)
# start ncurses, read data from r

This works. But it seems a little weird to create a pipe to destroy it right away and for the only purpose of creating an IO object.

Any suggestions of a nicer way to do this? Or is this a valid use case for supporting IO#dup, at least on the UNIX like platform?

Thanks,
Guillaume.

How to do the same thing in Ruby? There is no IO#dup or IO#dup2. Only
IO#reopen.

moulon% ruby -e 'p $stdin,$stdin.dup'
#<IO:0xb7d78040>
#<IO:0xb7d71f38>
moulon%

···

--

Guy Decoux

> How to do the same thing in Ruby? There is no IO#dup or IO#dup2. Only
> IO#reopen.

moulon% ruby -e 'p $stdin,$stdin.dup'
#<IO:0xb7d78040>
#<IO:0xb7d71f38>
moulon%

Duh!

I got confused by the documentation. ri IO#dup doesn't return anything and I thought that IO#dup would be Object#dup, i.e. it would not do a file descriptor dup. But it does:

[gus@genome1] 1 1 ~% ruby -e 'p $stdin.fileno,$stdin.dup.fileno'
0
3

Greping the source of Ruby for rb_cIO, I cannot find the place where IO#dup is defined in the IO class. I do see the function ruby_dup, but this is an internal function that works on int, not VALUE. I am confused...

Could the ri documentation be fixed? I would summit a patch if I knew where to insert the documentation...

Thanks Guy.

Guillaume.

···

Le 16 sept. 06, à 09:51, ts a écrit :

--

Guy Decoux

Greping the source of Ruby for rb_cIO, I cannot find the place where
IO#dup is defined in the IO class. I do see the function ruby_dup, but
this is an internal function that works on int, not VALUE. I am
confused...

#dup use #initialize_copy : rb_io_init_copy()

Guy Decoux

Got it. Thanks.

Guillaume.

···

Le 16 sept. 06, à 11:43, ts a écrit :

> Greping the source of Ruby for rb_cIO, I cannot find the place where
> IO#dup is defined in the IO class. I do see the function ruby_dup, but
> this is an internal function that works on int, not VALUE. I am
> confused...

#dup use #initialize_copy : rb_io_init_copy()

Guy Decoux