I have written a ruby script that then calls another ruby script. The callee script is very long and has a lot of “gets” for input.
So what I do is open my unix terminal, call the caller script, which then does this simple line:
load "calleeScript.rb"
The calleeScript.rb has been simplified to just do this:
input = gets.chomp
print input
But it just gives me an error, like it can’t handle gets. The error says:
./getsTest.rb:3:in `gets’: No such file or directory – 5 (Errno::ENOENT)
from ./getsTest.rb:3
Even if I take out that gets it won’t print/puts to the terminal. So any idea how I call one script that then calls another script (either relinquishing total control or forking), do some inputs/outputs, and still return to the previous script?
Kernel#getsis a convenience method that allows you to handle input both via standard input and via files.If you call your script without any arguments, i.e. like this:
Then
Kernel#getsreads its input from standard input. If, however, you call your script like this:Then
Kernel#getsreads its input from a file namedfoo.txt.In your case,
Kernel#getsis complaining that it can’t find a file named5, so presumably you called your script something like this:If you want to read from some specific source, you should call
IO#getson that specific source. I suggest callingNote: technically speaking, this is not a feature of
Kernel#getsbut rather of theARGFmagic constant. Basically,Kernel#getsjust callsARGF.gets.