Forum: Ruby Someone explain to me the logic behind ARGV[0]

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.
Cd204ed14b0f7475804f37171dd5a75a?d=identicon&s=25 Kaye Ng (marlon)
on 2010-12-18 11:12
For example there's a code
lines = File.readlines(ARGV[0])
Why do i need the [0]?

I have another program, testingargv.rb with this code
puts ARGV.join('-')
(no [0] parameter)
if i type in the command prompt, testingargv.rb test 123
the result is test-123

i don't understand the logic behind [0] in
lines = File.readlines(ARGV[0])

thanks guys!
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2010-12-18 12:06
(Received via mailing list)
On Saturday 18 December 2010 19:12:57 Kaye Ng wrote:
> i don't understand the logic behind [0] in
> lines = File.readlines(ARGV[0])
>
> thanks guys!

ARGV is an array. Calling ARGV[0] returns the first argument of the
array,
that is the first argument passed on the command line to the ruby
script.
Calling ARGV.join('-') returns a string obtained by concatenating the
elements
of the array with a - between each two of them.

The line

lines = File.readlines(ARGV[0])

assumes that the first parameter passed to the script on the command
line is
the name of the file, so ARGV[0] contains the name of the file and
File.readlines(ARGV[0]) returns an array of the lines contained in that
file.

For more information, see ri Array#[] and ri Array#join

Stefano
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2010-12-19 04:44
"Why do i need the [0]?"

The [0] just accesses the first member of the array, like for any other
array.
8498263b5390edb6749881639c9374d1?d=identicon&s=25 Alec Ross (Guest)
on 2010-12-21 23:45
(Received via mailing list)
In message <4C1A26450D42B303@smtp205.alice.it>, Stefano Crocco
<stefano.crocco@alice.it> writes
>On Saturday 18 December 2010 19:12:57 Kaye Ng wrote:
...

>> i don't understand the logic behind [0] in
>> lines = File.readlines(ARGV[0])
>>
>> thanks guys!
>
>ARGV is an array. Calling ARGV[0] returns the first argument of the array,

...

This naming and usage stems back to the C programming language. There,
argv is the conventional name for any command line arguments to the
program, and it denotes an array of c-style strings.  Elements of an
array in C are accessed by a (0-based) index.  Hence argv[0] gets the
first argument value, argv[1] the second, and so on.

HTH,

Alec
09a32175057418748822c587ac08c429?d=identicon&s=25 Abinoam Jr. (abinoampraxedes_m)
on 2010-12-22 04:20
(Received via mailing list)
Attachment: argv.rb (155 Bytes)
Just in case it's not clear enough for him (Kaye ng)...

In C argv[0] is the name of file being executed and argv[1] is the
actual "first" argument.
In Ruby ARGV[0] is like argv[1] in C, the actual first argument.

Play around a bit with a script like this.

p ARGV
ARGV.each_with_index do |x,y|
  puts "#{y} - #{x}"
end

Abinoam Jr.
This topic is locked and can not be replied to.