What's the 'portable' way to do this?

Hi,

I want to know if there’s a more ‘portable’ way to do this in ruby:

I hope my programe will accept data from redirected stdin or pipe, like

cat bar | foo.rb
foo.rb < bar

And after read the data I want to interact with the foo.rb in console.
The problem is stdin has been redirected so I have to:

STDIN.reopen(’/dev/tty’)

Hardcoding a string in the script seems not good. Any suggestions?

Thanks
Jan

On Apr 17, 2008, at 9:30 PM, Xie H. wrote:

STDIN.reopen(’/dev/tty’)

stdin = STDIN.dup

data = STDIN.read

STDIN.reopen stdin

a @ http://codeforpeople.com/

Thanks for your reply, but this seems not work.

I think the problem is when you do STDIN.dup, you’re duplicating the
already redirected STDIN.

On Apr 18, 2008, at 1:51 AM, Xie H. wrote:

Thanks for your reply, but this seems not work.

I think the problem is when you do STDIN.dup, you’re duplicating the
already redirected STDIN.

yeah, you’ll need to start out with a tty - maybe taking in the input
file as an argument or option.

a @ http://codeforpeople.com/

Hi,

I’m afraid the problem is the same: you can’t interact with the program
after data has been read. any function like getch() will try to read
input
from the file to which stdin was redirected instead of your console.

Thanks
Jan

Hi,

I think ARGF might work for your needs:

data = ARGF.read

Give it a try,
Dan

On Sat, Apr 19, 2008 at 1:40 PM, Xie H. [email protected]
wrote:

Hi,

I’m afraid the problem is the same: you can’t interact with the program
after data has been read. any function like getch() will try to read input
from the file to which stdin was redirected instead of your console.

You can check if you are getting data from a pipe by asking
$stdin.tty? and act accordingly.
See http://p.ramaze.net/1144 for a short example.

Regarding platform compatibility, according to
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/146259
you can $stdin.reopen(“COM1:”)

^ manveru

Hi,

On Sat, Apr 19, 2008 at 8:55 PM, James T. [email protected]
wrote:

Make that CON: and you’re on to a winner!

Completely OT here. I had this message open on the screen while I was
showing my mother something on my desk. We talked about for a bit and I
showed her whatever it was, and then she went to leave my room. As she
left,
she looked back at me and said, “Make that con and you’re onto a
winner!”. I
had no idea what she was saying, and asked “what?”. She pointed to the
screen and said, “… whatever that means.”

The things that happen!
Arlen

On 19 Apr 2008, at 10:55, Michael F. wrote:

You can check if you are getting data from a pipe by asking
$stdin.tty? and act accordingly.
See http://p.ramaze.net/1144 for a short example.

Regarding platform compatibility, according to
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/146259
you can $stdin.reopen(“COM1:”)

Make that CON: and you’re on to a winner!

dev = ‘CON:’
[$stdin, $stdout].each {|io| io.reopen(dev)}
puts gets

:slight_smile:

dev = ‘CON:’
[$stdin, $stdout].each {|io| io.reopen(dev)}
puts gets

Not works here, only on windows?

Actually I find this:
http://www.a-k-r.org/ruby-terminfo/rdoc/classes/TermInfo.html#M000031

Thanks all guys :slight_smile:
Jan

On 20 Apr 2008, at 12:40, Xie H. wrote:

program
Regarding platform compatibility, according to
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/146259
you can $stdin.reopen(“COM1:”)

Make that CON: and you’re on to a winner!

dev = ‘CON:’
[$stdin, $stdout].each {|io| io.reopen(dev)}
puts gets

Not works here, only on windows?

Yes ‘CON:’ applies to windows, not to *nix. On other platforms, most
of the time it should be /dev/tty.

dev = File::stat(’/dev/tty’) && ‘/dev/tty’ rescue ‘CON:’
[$stdin, $stdout].each { |io| io.reopen(dev) }
puts gets

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs