Herms Herms - 2 months ago 12
Ruby Question

Why is gets throwing an error when arguments are passed to my ruby script?

I'm using

to pause my script's output until the user hits the enter key. If I don't pass any arguments to my script then it works fine. However, if I pass any arguments to my script then gets dies with the following error:

ruby main.rb -i
main.rb:74:in `gets': No such file or directory - -i (Errno::ENOENT)
from main.rb:74:in `gets'

The error message is showing the argument I passed to the script. Why would gets be looking at ARGV?

I'm using OptionParser to parse my command line arguments. If I use
instead of
(so it removes things it parses from the argument list) then the application works fine.

So it looks like gets is reading from ARGV for some reason. Why? Is this expected? Is there a way to get it to not do that (doing
didn't help).


Ruby will automatically treat unparsed arguments as filenames, then open and read the files making the input available to ARGF ($<). By default, gets reads from ARGF. To bypass that:


It has been suggested that you could use STDIN instead of $stdin, but it's usually better to use $stdin.

Additionally, after you capture the input you want from ARGV, you can use:


Then you'll be free to gets without straggling data interfering.