[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Daniel Finnie

4/18/2008 5:08:00 PM

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

4 Answers

Michael Fellinger

4/19/2008 9:56:00 AM

0

On Sat, Apr 19, 2008 at 1:40 PM, Xie Hanjian <jan.h.xie@gmail.com> 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.ramaz... for a short example.

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

^ manveru

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

James Tucker

4/19/2008 10:56:00 AM

0


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:
>> 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.ramaz... for a short example.
>
> Regarding platform compatibility, according to
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
> 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

:-)

> ^ manveru


Arlen Cuss

4/19/2008 11:17:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

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

James Tucker

4/21/2008 9:58:00 AM

0


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

> * James Tucker <jftucker@gmail.com> [2008-04-19 19:55:45 +0900]:
>
>>
>> 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:
>>>> 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.ramaz... for a short example.
>>>
>>> Regarding platform compatibility, according to
>>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>>> 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

>
>
> Actually I find this:
> http://www.a-k-r.org/ruby-terminfo/rdoc/classes/TermInfo.ht...
>
> Thanks all guys :-)
> Jan
>
>>
>> :-)
>>
>>> ^ manveru
>>
>>
>
> --
> jan=callcc{|jan|jan};jan.call(jan)