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

···

--
jan=callcc{|jan|jan};jan.call(jan)

stdin = STDIN.dup

data = STDIN.read

STDIN.reopen stdin

a @ http://codeforpeople.com/

···

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

STDIN.reopen('/dev/tty')

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Hi,

I think ARGF might work for your needs:

data = ARGF.read

Give it a try,
Dan

···

On 4/17/08, Xie Hanjian <jan.h.xie@gmail.com> wrote:

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

--
jan=callcc{|jan|jan};jan.call(jan)

* ara.t.howard <ara.t.howard@gmail.com> [2008-04-18 13:58:31 +0900]:

STDIN.reopen('/dev/tty')

stdin = STDIN.dup

data = STDIN.read

STDIN.reopen stdin

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 17, 2008, at 9:30 PM, Xie Hanjian wrote:

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama

--
jan=callcc{|jan|jan};jan.call(jan)

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

* Daniel Finnie <dan@danfinnie.com> [2008-04-19 02:08:11 +0900]:

···

Hi,

I think ARGF might work for your needs:

data = ARGF.read

Give it a try,
Dan

On 4/17/08, Xie Hanjian <jan.h.xie@gmail.com> wrote:
> 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
>
>
> --
> jan=callcc{|jan|jan};jan.call(jan)
>
>

--
jan=callcc{|jan|jan};jan.call(jan)

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/

···

On Apr 18, 2008, at 1:51 AM, Xie Hanjian 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.

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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

···

On Sat, Apr 19, 2008 at 1:40 PM, Xie Hanjian <jan.h.xie@gmail.com> wrote:

Thanks
Jan

* Daniel Finnie <dan@danfinnie.com> [2008-04-19 02:08:11 +0900]:

> Hi,
>
> I think ARGF might work for your needs:
>
> data = ARGF.read
>
> Give it a try,
> Dan
>
> On 4/17/08, Xie Hanjian <jan.h.xie@gmail.com> wrote:
> > 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
> >
> >
> > --
> > jan=callcc{|jan|jan};jan.call(jan)
> >
> >
>

--

jan=callcc{|jan|jan};jan.call(jan)

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:")

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

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

:slight_smile:

···

On 19 Apr 2008, at 10:55, Michael Fellinger wrote:

On Sat, Apr 19, 2008 at 1:40 PM, Xie Hanjian <jan.h.xie@gmail.com> > wrote:

^ manveru

Hi,

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 Sat, Apr 19, 2008 at 8:55 PM, James Tucker <jftucker@gmail.com> wrote:

* James Tucker <jftucker@gmail.com> [2008-04-19 19:55:45 +0900]:

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:")

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?

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

Thanks all guys :slight_smile:
Jan

···

On 19 Apr 2008, at 10:55, Michael Fellinger wrote:

On Sat, Apr 19, 2008 at 1:40 PM, Xie Hanjian <jan.h.xie@gmail.com> wrote:

:slight_smile:

^ manveru

--
jan=callcc{|jan|jan};jan.call(jan)

* James Tucker <jftucker@gmail.com> [2008-04-19 19:55:45 +0900]:

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:")

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

···

On 20 Apr 2008, at 12:40, Xie Hanjian wrote:

On 19 Apr 2008, at 10:55, Michael Fellinger wrote:

On Sat, Apr 19, 2008 at 1:40 PM, Xie Hanjian <jan.h.xie@gmail.com> >>> wrote:

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

Thanks all guys :slight_smile:
Jan

:slight_smile:

^ manveru

--
jan=callcc{|jan|jan};jan.call(jan)