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.
L. M. (Guest)
on 2008-12-07 16: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
Tim H. (Guest)
on 2008-12-07 16: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.
L. M. (Guest)
on 2008-12-07 16:43
Tim H. wrote:

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

thank you!
Bob Schäfer (Guest)
on 2008-12-09 19:25
(Received via mailing list)
L. Maiwald wrote:
> Tim H. 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.