← Back to Ruby Course | Chapter 11: File I/O & Exceptions | Lesson 6 of 6

ARGV

In this page:

  1. ARGV

ARGV

ARGV is a global array holding the command-line arguments passed to a Ruby script when it's run, excluding the script name itself. ARGV[0] is the first argument, ARGV.length gives the count, and so on -- it behaves just like a normal array. This is the standard way a Ruby script reads input passed to it from the command line.

Note: ARGV always contains strings, even if an argument looks numeric -- convert with .to_i or .to_f if you need a number.

Example: ARGV

markup
# Simulating ARGV since this runs without real command-line arguments:
simulated_argv = ["report.txt", "--verbose"]

puts "Argument count: #{simulated_argv.length}"
puts "First argument: #{simulated_argv[0]}"
puts "In a real script, this data comes from the built-in ARGV array"
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.