You may know that Ruby sticks command-line arguments into a global array called ARGV. But why the heck is it called ARGV? It's an interesting history lesson that highlights Ruby's origins in C.

Argument Vector

ARGV stands for argument vector. And "vector" in this strange old-timey usage means "a one-dimensional array."

In C, every program has a main() function. It typically looks something like this:

int main(int argc, char *argv[]) {  
  ... your code here

As you probably noticed, the main function has two arguments. These are, respectively, a count and an array of command-line arguments.

When you tell bash to run your program, it does a system call which causes the OS to call your program's main function and to pass in certain argc and argv values.


The Ruby interpreter — MRI at least – is just a C program. Here is Ruby's main function:

main(int argc, char **argv)
    setlocale(LC_CTYPE, "");

    ruby_sysinit(&argc, &argv);
    return ruby_run_node(ruby_options(argc, argv));

As you can see, it passes argc and argv to a function called ruby_options, which in turn calls ruby_process_options, which calls process_options.

That handles all of the ruby interpreter options and eventually calls ruby_set_argv, which sets the ARGV you see in your ruby code.

ruby_set_argv(int argc, char **argv)
    int i;
    VALUE av = rb_argv;

#if defined(USE_DLN_A_OUT)
    if (origarg.argv)
    dln_argv0 = origarg.argv[0];
    dln_argv0 = argv[0];
    for (i = 0; i < argc; i++) {
    VALUE arg = external_str_new_cstr(argv[i]);

    rb_ary_push(av, arg);

Pretty neat. I'm still really new at diving into MRI codebase, but it's kind of fun to jump in and see exactly how things work.