From: ara.t.howard@... Date: 2006-04-25T12:07:55+09:00 Subject: Re: Format of RUBYOPT On Tue, 25 Apr 2006, Ryan Leavengood wrote: > On 4/24/06, ara.t.howard@noaa.gov wrote: >> >> if you look in ruby.c you'll see that it's literally parsed as part of the >> commandline - except only the switchs 'IdvwrK' and 'T' are accepted. it looks >> like it is indeed space separated. >> >> harp:~ > cat rubylib/a.rb >> A=42 >> >> harp:~ > RUBYOPT="-ryaml -Irubylib/ -ra" ruby -e "p [YAML, A]" >> [YAML, 42] >> >> seems to work. > > Hmmm, OK. Then why does setting RUBYOPT to just rubygems work (it has > no dash?) That doesn't work on the command-line (at least on Windows): > > C:\_Ryan\ruby_installer\svn\NSIS>ruby rubygems -e 'puts "hello"' > ruby: No such file or directory -- rubygems (LoadError) > > C:\_Ryan\ruby_installer\svn\NSIS>ruby -rubygems -e 'puts "hello"' > hello > > Maybe to be safe I should always just add -rubygems (i.e with a dash prefix.) ruby.c, in proc_options: 731 if (rb_safe_level() == 0 && (s = getenv("RUBYOPT"))) { 732 while (ISSPACE(*s)) s++; 733 if (*s == 'T' || (*s == '-' && *(s+1) == 'T')) { 734 int numlen; 735 int v = 1; 736 737 if (*s != 'T') ++s; 738 if (*++s) { 739 v = scan_oct(s, 2, &numlen); 740 if (numlen == 0) v = 1; 741 } 742 rb_set_safe_level(v); 743 } in the else block we eat the dash and any space, iff present. then we look for the opt char 744 else { 745 while (s && *s) { 746 if (*s == '-') { 747 s++; 748 if (ISSPACE(*s)) { 749 do {s++;} while (ISSPACE(*s)); 750 continue; 751 } 752 } 753 if (!*s) break; 754 if (!strchr("IdvwrK", *s)) 755 rb_raise(rb_eRuntimeError, "illegal switch in RUBYOPT: -%c", *s); now, given RUBYOPT=-rubygems, and *s==r, we call moreswitches 756 s = moreswitches(s); 757 } 758 } 759 } so in this function 433 static char* 434 moreswitches(s) 435 char *s; 436 { 437 int argc; char *argv[3]; 438 char *p = s; 439 440 argc = 2; argv[0] = argv[2] = 0; 441 while (*s && !ISSPACE(*s)) 442 s++; 443 argv[1] = ALLOCA_N(char, s-p+2); 444 argv[1][0] = '-'; 445 strncpy(argv[1]+1, p, s-p); 446 argv[1][s-p+1] = '\0'; 447 proc_options(argc, argv); 448 while (*s && ISSPACE(*s)) 449 s++; 450 return s; 451 } we eat whitespace, setup argv=[0, "-rubygems", 0] and argc == 2. then recurse into proc_options (the first method shown). so, as far as i can tell (and i've had three beers), proc_options finds the 'r', jumps into moreswitches which restores the '-', and the were back into proc_options with '-rubygems', which works because of the 'ubygems' hack. the parsing of RUBYOPT looks as if it ignores '-' or lack thereof be design, but i certainly can't be knowing. good luck! ;-) -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama