IO#dup

G

Guillaume Marcais

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.
 
T

ts

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

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

Guillaume Marcais

Le 16 sept. 06, =E0 09:51, ts a =E9crit :
G> How to do the same thing in Ruby? There is no IO#dup or IO#dup2.=20
Only
G> 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=20=

and I thought that IO#dup would be Object#dup, i.e. it would not do a=20
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=20
IO#dup is defined in the IO class. I do see the function ruby_dup, but=20=

this is an internal function that works on int, not VALUE. I am=20
confused...

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

Thanks Guy.

Guillaume.
 
T

ts

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...

#dup use #initialize_copy : rb_io_init_copy()


Guy Decoux
 
G

Guillaume Marcais

Le 16 sept. 06, =E0 11:43, ts a =E9crit :
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,=20=
but
G> this is an internal function that works on int, not VALUE. I am
G> confused...

#dup use #initialize_copy : rb_io_init_copy()


Guy Decoux

Got it. Thanks.

Guillaume.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top