Run this code, as ./test.rb. Then run it with an command line arg,
like “./test.rb -x”. And you will get an error like this:
“test.rb:3:in `gets’: No such file or directory - -x
(Errno::ENOENT)”
The fix I have found, is to change gets to $stdin.gets. I saw some
traffic on the mailing list archives regarding this, but no
resolution.
Is this expected behaviour?
Yes it is. Kernel.gets() reads from files specified as command-line
options or STDIN, if none were given. This makes it easy to write
Unix-style filters.
You can always shift the option out of ARGV before calling gets()
with something like:
Run this code, as ./test.rb. Then run it with an command line arg, like
Thanks,
~S
I’ve run into this problem before as well, somebody pointed me to the ri
documentation on Kernel#gets, it says the following.
Returns the next line from the list of files in +ARGV+, or from standard
input if no files are present on the command line. Returns +nil+ at end
of file.