argc *argv[] in Ruby

R

RaW

Hello,
Does Ruby have something like C has argc and *argv[]?
I need to run ruby script with a command line parameters.

Thanks in advance for any helpfull answer.
 
C

Chris Eskow

------=_Part_454_31610721.1128983454510
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

ARGV holds an array of command line parameters. There's no argc, but you ca=
n
just use ARVG.length.

------=_Part_454_31610721.1128983454510--
 
C

Chris Eskow

------=_Part_534_1012094.1128983679553
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Oh, one more thing: Unlike argv in C/C++, in Ruby ARVG[0] will contain the
first argument passed in to the program, not the program name. If you want
the name of script, use the global variable $0.

------=_Part_534_1012094.1128983679553--
 
R

Robert Klemme

Chris said:
Oh, one more thing: Unlike argv in C/C++, in Ruby ARVG[0] will
contain the first argument passed in to the program, not the program
name. If you want the name of script, use the global variable $0.

Adding to that there's also ARGF which is a single IO like object that
reads from all command line named files:

# poor man's cat
# this works even without any arguments by
# reading from STDIN
ARGF.each_line {|line| puts line}

And while we're at it, there are also two libs for option processing:

http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html
http://www.ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/index.html

I recommend OptionParser as it seems more flexible and also has very nice
help output generation.

Kind regards

robert
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top