Redson
Redson

Reputation: 2140

How to check the number of arguments passed with a Ruby Script

I would like to know how to get the number of arguments pass with a ruby script. So far everything I searched for has been to functions.

I execute my script like this: ruby script.rb Arg1 Arg2 Arg3

Now I want to get the number of arguments, in this case it is 3. How can I do so?

I am using ruby1.9.3

EDIT: I want my code to look like:

#!/usr/bin/env ruby

# check number of arguments passed

.
.
.

Upvotes: 3

Views: 5714

Answers (2)

Lesleh
Lesleh

Reputation: 1675

ARGV contains all the arguments passed to the script, ARGV.length is the number of arguments.

Upvotes: 8

Chris Heald
Chris Heald

Reputation: 62668

You're looking for the ARGV array. It's just like any other array, but it's populated with a list of command line arguments passed.

http://ruby-doc.org/core-1.9.3/ARGF.html

Upvotes: 2

Related Questions