0

I'm new to ruby and this issue is bugging me for a while . Whenever i use gets to take user input , my gets statement is executed right after i run the file .I'm using git Bash to run my file.rb file ,

puts "some unnecessary text"
puts "Hello world"
puts "now you should input something" 
x = gets.chomp
puts 36
puts "your input is " + x + " right?"

the program should print the first 3 lines before waiting for an input but it waits for the input right after i run it

$ruby file.rb
|

it waits for eternity unless I press enter . If i write something,

$ ruby file.rb
myInput
some unnecessary text
Hello world
now you should input something
36
your input is myInput right?

it runs okay . So I'm forced to write my input at the beginning . It's not much of a problem right now but it'll cause a lot if headaches when i write bigger and more complex code . Any solutions ?

ps: It seems the problem only occurs with git Bash (windows) . Powershell works just fine .

  • hmm. can't replicate -- works as expected in OSX. run file, prints three lines, waits for input with an enter, prints remaining two lines. What is your OS and ruby version? – dbugger Aug 06 '20 at 16:24
  • I'm having trouble figuring out what this has to do with Rails, and where the compiler errors are. – Jörg W Mittag Aug 06 '20 at 16:59
  • @JörgWMittag oops sorry , those was automatically suggested – Sal the Sad manShark Aug 06 '20 at 17:29
  • This sounds like an OS issue -- check this out https://stackoverflow.com/questions/33883530/why-is-my-command-prompt-freezing-on-windows-10 – dbugger Aug 06 '20 at 17:36
  • It seems that the standard output is buffered, try to put at the beginning of the file (first two lines) `old_sync = $stdout.sync $stdout.sync = true` and athe end of the file (last line) `$stdout.sync = old_sync` if this trick works at least you know the reason of the weird behaviour see [this](https://stackoverflow.com/questions/39920517/how-to-print-stdout-immediately) SO post for details. – Giuseppe Schembri Aug 06 '20 at 18:43
  • @GiuseppeSchembri this works , thanks . Could you please repost your comment as an answer so it'd be easier to find for other people. – Sal the Sad manShark Aug 07 '20 at 10:24
  • @SaltheSadmanShark Done. – Giuseppe Schembri Aug 07 '20 at 16:14

1 Answers1

0

It seems that the standard output is buffered.

Try to put at the beginning of the file (first two lines) old_sync = $stdout.sync $stdout.sync = true and a the end of the file (last line) $stdout.sync = old_sync.

The call to the IO#sync= method set the sync mode to true. This cause that all output is immediately flushed, at the end of the script we restore its value to its original old value, see Ruby documentation for details.

In summary:

old_sync = $stdout.sync # cache old value
$stdout.sync = true # set mode to true

# your scripting staff

$stdout.sync = old_sync # restore old value

If this trick works at least you know the reason for the weird behaviour. You can find some explanation also in this SO post.