[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

gets does not block (STDOUT.flush issue?

Ivan B

6/1/2007 5:00:00 AM

File: test.rb


STDOUT.sync=true

puts "File exists. Are you sure?"
STDOUT.flush
answer=gets.chomp
puts answer

executing this file with any parameters (e.g. ./test.rb bla) yields
this:

[:~/ruby]$ ./test.rb bla
File exists. Are you sure?
/test.rb:9:in `gets': No such file or directory - "bla" (Errno::ENOENT)
from ./test.rb:9
[:~/ruby]$

What gives?!

--
Posted via http://www.ruby-....

1 Answer

Brian Candler

6/1/2007 5:35:00 AM

0

On Fri, Jun 01, 2007 at 01:59:33PM +0900, Ivan B wrote:
> STDOUT.sync=true
>
> puts "File exists. Are you sure?"
> STDOUT.flush
> answer=gets.chomp
> puts answer
>
> executing this file with any parameters (e.g. ./test.rb bla) yields
> this:
>
> [:~/ruby]$ ./test.rb bla
> File exists. Are you sure?
> ./test.rb:9:in `gets': No such file or directory - "bla" (Errno::ENOENT)
> from ./test.rb:9
> [:~/ruby]$
>
> What gives?!

You need:

answer=$stdin.gets.chomp

Ohterwise, gets tries to read from file(s) whose name(s) are given on the
command line.