if (ARGV[0] == 'add')
puts 'input something'
gt = gets
puts gt
else
puts 'fail'
end
If I try to execute above program am getting below error:
C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
input something
test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
from test.rb:3
C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
input something
test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
from test.rb:3
Please let me know why is this error coming?
Kernel#gets tries to read from the file specified by the command line
arguments if there are any. This is documented (and in some cases useful).
If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).
if (ARGV.shift == 'add')
puts 'input something'
gt = gets
puts gt
else
puts 'fail'
end
A more complex but also more robust alternative might be to process
command line options e.g. with OptionParser.
Kind regards
robert
···
2008/2/20, Sebastian Hungerecker <sepp2k@googlemail.com>:
ycsunil wrote:
> C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
> input something
> test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
> from test.rb:3
>
> Please let me know why is this error coming?
Kernel#gets tries to read from the file specified by the command line
arguments if there are any. This is documented (and in some cases useful).
If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).
--
use.inject do |as, often| as.you_can - without end