How does ARGV get set in Ruby?

You probably know that Ruby sticks any 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.

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.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. 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
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like: