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.

Ruby

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

int
main(int argc, char **argv)
{
#ifdef RUBY_DEBUG_ENV
    ruby_set_debug_option(getenv("RUBY_DEBUG"));
#endif
#ifdef HAVE_LOCALE_H
    setlocale(LC_CTYPE, "");
#endif

    ruby_sysinit(&argc, &argv);
    {
    RUBY_INIT_STACK;
    ruby_init();
    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.

void
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];
    else
    dln_argv0 = argv[0];
#endif
    rb_ary_clear(av);
    for (i = 0; i < argc; i++) {
    VALUE arg = external_str_new_cstr(argv[i]);

    OBJ_FREEZE(arg);
    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.

Get the Honeybadger newsletter

Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo
    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    An advertisement for Honeybadger that reads 'Turn your logs into events.'

    "Splunk-like querying without having to sell my kidneys? nice"

    That’s a direct quote from someone who just saw Honeybadger Insights. It’s a bit like Papertrail or DataDog—but with just the good parts and a reasonable price tag.

    Best of all, Insights logging is available on our free tier as part of a comprehensive monitoring suite including error tracking, uptime monitoring, status pages, and more.

    Start logging for FREE
    Simple 5-minute setup — No credit card required