rubycmdwindows-10stdin

Still receiving Invalid Argument Errno::EINVAL with STDIN.gets.chomp and $stdin.gets.chomp - Windows 10


I've never written or read any ruby code in my life until today - this said - I've been tasked with solving an input problem. In the short amount of reading I've done, I've found that @vars are Instance Variables and I've also read that gets will invariably attempt to read any arguments passed to the .rb script in use, and thus, using ARGV.clear would clear that and allow STDIN be used to receive input from a keyboard. An important note is we're forced to use Ruby 1.9.2-p180 because the original author wrote it for this version and nobody here has the time to refactor for newer versions.

Consider the following Batch Script:

@Echo Off
Title Call Some Ruby
CMD /K ruby FuBar.rb

Fairly simple!

Inside the FuBar.rb script, I've got a code block that equivalates to what I'd see in a set /p "var=something" line in Batch followed by something akin to select case that I'd see in VB etc.:

@input = gets.chomp
case @input
  when "q"
    break
  else
    if (@input.to_i >=0 && @input.to_i < @tests.size)
      executeTest(@input.to_i)
    else
      puts "Wrong test number."
    end
end

Everything seems straight forward, but the problem I'm getting is on the gets line.

I receive this error:

 > FuBar.rb:55:in 'gets': Invalid argbument - <STDIN> <Errno::EINVAL>
          from FuBar.rb:55:in 'gets'
          from FuBar.rb:55:in '<main>'

Now - in the actual "FuBar.rb" script, that's where the @input = gets.chomp line exists. Adding ARGV.clear makes no difference, and when added, the error changes from rb:55 to rb:56 naturally.

Interestingly, if I use @input = STDIN.gets.chomp or @input = $stdin.gets.chomp instead, my error changes a little bit, but ultimately stays the same - the secondary gets line is removed.

 > FuBar.rb:55:in 'gets': Invalid argbument - <STDIN> <Errno::EINVAL>
          from FuBar.rb:55:in '<main>'

My assumption as based on a comment in this question, is that for some reason, calling the ruby script from the batch file isn't allowing it to be an Interactive Shell, but here's the kicker . . . it works just fine on another Computer with the same environment (as far as I can tell - again - I'm new to ruby)

What am I missing?


Solution

  • Because Ruby 1.9.2-p180 is very old and the latest 1.9-series patch is 1.9.3-p551, it's possible the issue is simply a bug in the 1.9 series that has been fixed in one of the numerous later patches.

    Therefore it's worth testing the newest patch first before trying anything else. The issue might just have been fixed in 1.9.3-p551.