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?
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:
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…
G> Greping the source of Ruby for rb_cIO, I cannot find the place where
G> IO#dup is defined in the IO class. I do see the function ruby_dup,
but
G> this is an internal function that works on int, not VALUE. I am
G> confused…