Вопрос

I've been running quite a few ruby console scripts in cygwin terminal under windows xp with no problems so far using ruby 1.9.3-p28 from rubyinstaller.

All of a sudden now I'm noticing this odd behaviour. I have the following simple code:

while input = gets
  puts input
  puts "ok"
end

which gives no output whatsoever in console. It seems the input = gets part makes the while cycle hang for some reason. If I change the script this way

while true
  puts "ok"
end

while cycle works as expected, but if I add the line input = gets just below puts "ok" it's hanging again, and strange thing is it doesn't even write "ok" to console just before hanging. It seems it's doing completely nothing and windows task manager shows ruby process has no activity at all.

I've also tried with loop do end cycle, but behaviour is exactly the same.

Instead, things works ok if I use no cycle at all.

All I can understand is that it's related to stdin gets method inside a cycle.

I'd like if someone could help me debug what is exactly happening here.

UPDATE (more info): script correctly works in windows command shell.

Это было полезно?

Решение

I fixed it with:

require 'win32console' if RbConfig::CONFIG['host_os'].include? "mingw32"

I didn't know that I had to use "win32console" gem with a cygwin terminal even in the case that I I am not outputting colors as so far I have been using "win32console" only togheter with "colorize" gem to make colors work in windows xp native command prompt.

I'll stick a reminder for this at the top of my code...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top