Forum: Ruby newbie's question: getting parameters given to hello.rb file

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.
08303fdbb0ce75d2e6cb823e3da6edf3?d=identicon&s=25 L. Maiwald (maiwald)
on 2008-12-07 15:16
hello,

given i have a programme "hello.rb" and i execute it in terminal like
this

./hello.rb lalala lala stuff

how do i get the "lalala lala stuff" into my programme?

thanks
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2008-12-07 15:34
(Received via mailing list)
L. Maiwald wrote:
> hello,
>
> given i have a programme "hello.rb" and i execute it in terminal like
> this
>
> ./hello.rb lalala lala stuff
>
> how do i get the "lalala lala stuff" into my programme?
>
> thanks

The arguments are in the ARGV array. ARGV[0] is the first argument,
ARGV[1] the second, and so forth.
08303fdbb0ce75d2e6cb823e3da6edf3?d=identicon&s=25 L. Maiwald (maiwald)
on 2008-12-07 15:43
Tim Hunter wrote:

> The arguments are in the ARGV array. ARGV[0] is the first argument,
> ARGV[1] the second, and so forth.

thank you!
Cc006e407ec2957c16138b361a5a146f?d=identicon&s=25 Bob Schäfer (Guest)
on 2008-12-09 18:25
(Received via mailing list)
L. Maiwald wrote:
> Tim Hunter wrote:
>
>> The arguments are in the ARGV array. ARGV[0] is the first argument,
>> ARGV[1] the second, and so forth.
>
> thank you!

additionally, sometimes it's helpful to know the number of arguments
sent in with

ARGV.size

when my shell-style ruby scripts are dependent on arguments, i include a
line at the top like this:

raise 'usage: rubycat.rb file1 file2' if ARGV.size == 0
This topic is locked and can not be replied to.