[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ARGV, STDIN and gets

Joe Williams

4/24/2009 10:43:00 PM

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.ht...). 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


--
Name: Joseph A. Williams
Email: joe@joetify.com
Blog: http://www.joeandmoto...


3 Answers

Nobuyoshi Nakada

4/24/2009 10:53:00 PM

0

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.

--
Nobu Nakada

Joel VanderWerf

4/24/2009 11:01:00 PM

0

Joe Williams wrote:
> 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.ht...). 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

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"]

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joe Williams

4/24/2009 11:47:00 PM

0

ARGV.shift works perfectly, should have thought of that.

Thanks!

-Joe


Joel VanderWerf wrote:
> Joe Williams wrote:
>> 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.ht...). 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
>
> 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"]
>

--
Name: Joseph A. Williams
Email: joe@joetify.com
Blog: http://www.joeandmoto...