Reopening stdin

Hi!

I have a script.rb which reads some data from stdin.
Something like this:

bash$ cat file_name | script.rb

In script.rb I have:

···

#--------------------
fi = $stdin

fi.each_line do |l|
  #...
end

--------------------

After that script.rb needs to communicate with a user interactively:
ask what to do and read the answer from... stdin. Something like:

#-------------------

print "\nYes/No: "
answer = $stdin.gets

#-------------------

Here I had a problem - I can't read user's answer.
I guess I need somehow to reopen stdin before calling
$stdin.gets but have not found how to do it correctly.

Could you please give me a hint?

Thanks a lot!

--
   S&G

well, here you setup stdin of the script.rb to be a pipe and therefore it has
no controlling terminal:

   bash$ cat file_name | script.rb

so, even if you reopen it it will still be a pipe - you cannot get back at the
terminal. i think what you are saying is that you want to open the console
after reading stdin. so maybe:

   harp:~ > cat a.rb
   print STDIN.read
   STDIN.reopen '/dev/tty'
   print STDIN.read

   harp:~ > printf "42\n" | ruby a.rb
   42
   then i type 42 followed by ctrl-d

hth.

-a

···

On Tue, 13 Sep 2005, Sea&Gull wrote:

Hi!

I have a script.rb which reads some data from stdin. Something like this:

bash$ cat file_name | script.rb

In script.rb I have:

#--------------------
fi = $stdin

fi.each_line do |l|
  #...
end

--------------------

After that script.rb needs to communicate with a user interactively: ask
what to do and read the answer from... stdin. Something like:

#-------------------

print "\nYes/No: "
answer = $stdin.gets

#-------------------

Here I had a problem - I can't read user's answer. I guess I need somehow
to reopen stdin before calling $stdin.gets but have not found how to do it
correctly.

Could you please give me a hint?

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

Ara.T.Howard wrote:

···

On Tue, 13 Sep 2005, Sea&Gull wrote:

Hi!

I have a script.rb which reads some data from stdin. Something like this:

bash$ cat file_name | script.rb

In script.rb I have:

#--------------------
fi = $stdin

fi.each_line do |l|
    #...
end

--------------------

After that script.rb needs to communicate with a user interactively: ask
what to do and read the answer from... stdin. Something like:

#-------------------

print "\nYes/No: "
answer = $stdin.gets

#-------------------

Here I had a problem - I can't read user's answer. I guess I need somehow
to reopen stdin before calling $stdin.gets but have not found how to do it
correctly.

Could you please give me a hint?

well, here you setup stdin of the script.rb to be a pipe and therefore it has
no controlling terminal:

  bash$ cat file_name | script.rb

so, even if you reopen it it will still be a pipe - you cannot get back at the
terminal. i think what you are saying is that you want to open the console
after reading stdin. so maybe:

  harp:~ > cat a.rb
  print STDIN.read
  STDIN.reopen '/dev/tty'
  print STDIN.read

  harp:~ > printf "42\n" | ruby a.rb
  42
  then i type 42 followed by ctrl-d

hth.

-a

Yes, it was exactly what I was asking
about (with slightly wrong formulation :slight_smile: )

Thanks, you helped me a lot!

--
   S&G