Forum: Ruby ARGV, STDIN and gets

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da66a56107b535386a9daf538a3b1d7d?d=identicon&s=25 Joe Williams (zeusfaber)
on 2009-04-25 00:44
(Received via mailing list)
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
(http://www.ruby-doc.org/core/classes/Kernel.html#M005996). Is there a
way around this so that I can pass in arguments and listen on stdin as
well. Running the script without args works perfectly. What I am doing
currently looks something like the following:

var1 = ARGV[0]
var2 = ARGV[1]
var3 = gets

Any advice would be helpful.

Thanks.
-Joe
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2009-04-25 00:52
(Received via mailing list)
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.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-04-25 01:01
(Received via mailing list)
Joe Williams wrote:
> var3 = gets
>
> Any advice would be helpful.
>
> Thanks.
> -Joe

Kernel#gets reads from ARGF, which, if there's anything in ARGV, is read
from those files. So you can clear from ARGV first, if you want the
option of passing your file as the 3rd arg instead of stdin.

$ cat tt.rb
var1 = ARGV.shift
var2 = ARGV.shift
var3 = gets
p [var1, var2, var3]

$ cat data
this is some text
$ ruby tt.rb foo bar data
["foo", "bar", "this is some text\n"]
$ ruby tt.rb foo bar
type some input
["foo", "bar", "type some input\n"]
Da66a56107b535386a9daf538a3b1d7d?d=identicon&s=25 Joe Williams (zeusfaber)
on 2009-04-25 01:47
(Received via mailing list)
ARGV.shift works perfectly, should have thought of that.

Thanks!

-Joe
7eabdb827390e9f4a2064727a3205331?d=identicon&s=25 Prakash Murthy (prakblr)
on 2010-08-03 03:24
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!
79f35e001c5fcb6856cfa883d0efcc83?d=identicon&s=25 Daniel C. (daniel_c61)
on 2011-05-25 07:05
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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2011-05-25 11:54
(Received via mailing list)
On Wed, May 25, 2011 at 7:05 AM, Daniel C. <daniel_antonio_n@yahoo.com>
wrote:
>>> Kernel#gets is not a method to read from stdin.
> 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
This topic is locked and can not be replied to.