From: Robert Klemme Date: 2011-05-25T18:53:29+09:00 Subject: Re: ARGV, STDIN and gets On Wed, May 25, 2011 at 7:05 AM, Daniel C. wrote: > Prakash Murthy wrote in post #929972: >> Nobuyoshi Nakada wrote: >>> Hi, >>> >>> At Sat, 25 Apr 2009 07:43:28 +0900, >>> Joe Williams wrote in [ruby-talk:334959]: >>>> I am running into an issue where I have a script that takes two command >>>> line arguments and reads from stdin. It seems that gets is not getting >>>> to stdin because it is hitting ARGV first >>> >>> Kernel#gets is not a method to read from stdin. >>> >>> Use $stdin.gets. >> >> Thanks Nobuyoshi! >> >> I used $stdin.gets. instead of gets. and that solved the problem I was >> facing! > > The solution is more very easy, like this: > > var2 = ARGV[1].clone > ARGV.clear > var3 = gets > > its work! not need STDIN! :) no error more. Why do you clone when the next thing that you do is clearing ARGV? That seems superfluous. For one argument I would do arg = ARGV.shift or abort "ERROR: need at least one argument" For more arguments I'd probably do arg1, arg2 = ARGV.slice! 0, 2 abort "Need two arguments!" unless arg2 gets # reads stdin or from file if there are more arguments If there is a need for more complex argument processing I would use OptionParser anyway. OptionParser.new do |opts| ... end.parse! ARGV # removes options Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/