gets does not block (STDOUT.flush issue?)

I

Ivan B

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?!
 
B

Brian Candler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top