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

L

L. Maiwald

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
 
T

Tim Hunter

L. Maiwald said:
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.
 
B

Bob Schäfer

L. Maiwald said:
Tim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top