# Format of RUBYOPT

**URL:** <https://rubytalk.org/t/format-of-rubyopt/26917>\
**Category:** ruby-talk\
**Created:** [25 April 2006 01:23 UTC](https://rubytalk.org/t/format-of-rubyopt/26917 "2006-04-25T01:23:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [25 April 2006 01:23 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/1 "2006-04-25T01:23:56Z")

</div>

Hello all,

I'm working on fixing the remaining installer problems in the  
One-Click Installer so we can finally release it, and one of those  
involves the manipulation of the RUBYOPT environment variable. On that  
note, I was wondering the format of that. If there are multiple  
entrys, how are they formatted? Just spaces in between?

In other words, if I am to add rubygems as a new option while  
preserving the older options, what would I do? Just add it with a  
space prefix at the end of RUBYOPT?

In my testing there seemed to be cases when RUBYOPT was not read and I  
also got errors sometimes, so I want to ensure I don't break someone's  
system by setting an invalid RUBYOPT.

Thanks,  
Ryan

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [25 April 2006 02:07 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/2 "2006-04-25T02:07:26Z")

</div>

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.

&nbsp;&nbsp;&nbsp;harp:~ \> cat rubylib/a.rb  
&nbsp;&nbsp;&nbsp;A=42

&nbsp;&nbsp;&nbsp;harp:~ \> RUBYOPT="-ryaml -Irubylib/ -ra" ruby -e "p [YAML, A]"  
&nbsp;&nbsp;&nbsp;[YAML, 42]

seems to work.

cheers.

-a

> **···**
>
> On Tue, 25 Apr 2006, Ryan Leavengood wrote:
> 
> > Hello all,
> > 
> > I'm working on fixing the remaining installer problems in the  
> > One-Click Installer so we can finally release it, and one of those  
> > involves the manipulation of the RUBYOPT environment variable. On that  
> > note, I was wondering the format of that. If there are multiple  
> > entrys, how are they formatted? Just spaces in between?
> > 
> > In other words, if I am to add rubygems as a new option while  
> > preserving the older options, what would I do? Just add it with a  
> > space prefix at the end of RUBYOPT?
> > 
> > In my testing there seemed to be cases when RUBYOPT was not read and I  
> > also got errors sometimes, so I want to ensure I don't break someone's  
> > system by setting an invalid RUBYOPT.
> > 
> > Thanks,  
> > Ryan
> 
> --  
> be kind whenever possible... it is always possible.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [25 April 2006 02:11 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/3 "2006-04-25T02:11:26Z")

</div>

Responding to my own thread: Given that the above is true, I wrote  
Ruby script to work out the logic of this and here is the result:

$already\_defined = {}

def add\_to\_rubyopt(var, rubyopt)  
&nbsp;&nbsp;a = rubyopt.split(' ')  
&nbsp;&nbsp;if not a.include?(var)  
&nbsp;&nbsp;&nbsp;&nbsp;a \<\< var  
&nbsp;&nbsp;&nbsp;&nbsp;$already\_defined[var] = nil  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;$already\_defined[var] = true  
&nbsp;&nbsp;end  
&nbsp;&nbsp;a.join(' ')  
end

def del\_from\_rubyopt(var, rubyopt)  
&nbsp;&nbsp;if not $already\_defined[var]  
&nbsp;&nbsp;&nbsp;&nbsp;a = rubyopt.split(' ')  
&nbsp;&nbsp;&nbsp;&nbsp;a.delete(var)  
&nbsp;&nbsp;&nbsp;&nbsp;a.join(' ')  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;rubyopt  
&nbsp;&nbsp;end  
end

require 'test/unit'

class RubyOptTest \< Test::Unit::TestCase  
&nbsp;&nbsp;def test\_add\_to\_rubyopt  
&nbsp;&nbsp;&nbsp;&nbsp;var = 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;# Before and after tests  
&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['', var],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['w', "w #{var}"],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['w v somethinglong', "w v somethinglong #{var}"]  
&nbsp;&nbsp;&nbsp;&nbsp;].each do |before, after|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(after, add\_to\_rubyopt(var, before))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(before, del\_from\_rubyopt(var, after))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;# Here nothing should be changed  
&nbsp;&nbsp;&nbsp;&nbsp;[var, "w #{var}", "w v #{var} somethinglong"].each do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(s, add\_to\_rubyopt(var, s))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(s, del\_from\_rubyopt(var, s))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
\_\_END\_\_

If the logic of the above seems wrong in any way, let me know. Now the  
trick is writing the above in the NSIS scripting language (it's  
practically like assembler compared to Ruby, grrr.)

Ryan

> **···**
>
> On 4/24/06, Ryan Leavengood \<leavengood@gmail.com\> wrote:
> 
> > In other words, if I am to add rubygems as a new option while  
> > preserving the older options, what would I do? Just add it with a  
> > space prefix at the end of RUBYOPT?

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [25 April 2006 02:16 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/4 "2006-04-25T02:16:24Z")

</div>

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.)

Ryan

> **···**
>
> On 4/24/06, ara.t.howard@noaa.gov \<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.
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> cat rubylib/a.rb  
> > &nbsp;&nbsp;&nbsp;A=42
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> RUBYOPT="-ryaml -Irubylib/ -ra" ruby -e "p [YAML, A]"  
> > &nbsp;&nbsp;&nbsp;[YAML, 42]
> > 
> > seems to work.

---

<div class="post-metadata">

**Author:** ![Curt\_Work\_Hibbs1](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@Curt\_Work\_Hibbs1](https://rubytalk.org/u/Curt_Work_Hibbs1)\
**Post date:** [25 April 2006 02:22 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/5 "2006-04-25T02:22:07Z")

</div>

I only recently discovered this myself... with a single option you can omit  
the "-", but with multiple, the "-" is required.

Curt

> **···**
>
> On 4/24/06, Ryan Leavengood \<leavengood@gmail.com\> wrote:
> 
> > On 4/24/06, ara.t.howard@noaa.gov \<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.)
> > 
> > Ryan

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [25 April 2006 03:07 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/6 "2006-04-25T03:07:55Z")

</div>

ruby.c, in proc\_options:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;731 if (rb\_safe\_level() == 0 && (s = getenv("RUBYOPT"))) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;732 while (ISSPACE(\*s)) s++;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;733 if (\*s == 'T' || (\*s == '-' && \*(s+1) == 'T')) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;734 int numlen;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;735 int v = 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;736  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;737 if (\*s != 'T') ++s;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;738 if (\*++s) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;739 v = scan\_oct(s, 2, &numlen);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;740 if (numlen == 0) v = 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;741 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;742 rb\_set\_safe\_level(v);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;743 }

in the else block we eat the dash and any space, iff present. then we look  
for the opt char

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;744 else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;745 while (s && \*s) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;746 if (\*s == '-') {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;747 s++;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;748 if (ISSPACE(\*s)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;749 do {s++;} while (ISSPACE(\*s));  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;750 continue;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;751 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;752 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;753 if (!\*s) break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;754 if (!strchr("IdvwrK", \*s))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;755 rb\_raise(rb\_eRuntimeError, "illegal switch in RUBYOPT: -%c", \*s);

now, given RUBYOPT=-rubygems, and \*s==r, we call moreswitches

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;756 s = moreswitches(s);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;757 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;758 }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;759 }

so in this function

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;433 static char\*  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;434 moreswitches(s)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;435 char \*s;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;436 {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;437 int argc; char \*argv[3];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;438 char \*p = s;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;439  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;440 argc = 2; argv[0] = argv[2] = 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;441 while (\*s && !ISSPACE(\*s))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;442 s++;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;443 argv[1] = ALLOCA\_N(char, s-p+2);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;444 argv[1][0] = '-';  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;445 strncpy(argv[1]+1, p, s-p);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;446 argv[1][s-p+1] = '\0';  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;447 proc\_options(argc, argv);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;448 while (\*s && ISSPACE(\*s))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;449 s++;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;450 return s;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Tue, 25 Apr 2006, Ryan Leavengood wrote:
> 
> > On 4/24/06, ara.t.howard@noaa.gov \<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.
> > > 
> > > &nbsp;&nbsp;&nbsp;harp:~ \> cat rubylib/a.rb  
> > > &nbsp;&nbsp;&nbsp;A=42
> > > 
> > > &nbsp;&nbsp;&nbsp;harp:~ \> RUBYOPT="-ryaml -Irubylib/ -ra" ruby -e "p [YAML, A]"  
> > > &nbsp;&nbsp;&nbsp;[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.)
> 
> --  
> be kind whenever possible... it is always possible.  
> - h.h. the 14th dali lama

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [25 April 2006 02:28 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/7 "2006-04-25T02:28:53Z")

</div>

OK, so not only do I need to check for an existing RUBYOPT, but if is  
defined and it only has one option, I need to add a dash to the  
existing if it lacks it, then add -rubygems. Then on uninstall this  
needs to be restored. Nice. I'm glad I'm prototyping this in Ruby.

Ryan

> **···**
>
> On 4/24/06, Curt Hibbs \<ml.chibbs@gmail.com\> wrote:
> 
> > I only recently discovered this myself... with a single option you can omit  
> > the "-", but with multiple, the "-" is required.

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [25 April 2006 03:31 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/8 "2006-04-25T03:31:25Z")

</div>

To summarize, if you're going to alter RUBYOPT by appending something,  
you should append a space and then '-rrubygems', to be sure?

> **···**
>
> On 4/24/06, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Tue, 25 Apr 2006, Ryan Leavengood wrote:
> > 
> > \> On 4/24/06, ara.t.howard@noaa.gov \<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:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;731 if (rb\_safe\_level() == 0 && (s = getenv("RUBYOPT"))) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;732 while (ISSPACE(\*s)) s++;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;733 if (\*s == 'T' || (\*s == '-' && \*(s+1) == 'T')) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;734 int numlen;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;735 int v = 1;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;736  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;737 if (\*s != 'T') ++s;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;738 if (\*++s) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;739 v = scan\_oct(s, 2, &numlen);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;740 if (numlen == 0) v = 1;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;741 }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;742 rb\_set\_safe\_level(v);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;743 }
> > 
> > in the else block we eat the dash and any space, iff present. then we look  
> > for the opt char
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;744 else {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;745 while (s && \*s) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;746 if (\*s == '-') {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;747 s++;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;748 if (ISSPACE(\*s)) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;749 do {s++;} while (ISSPACE(\*s));  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;750 continue;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;751 }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;752 }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;753 if (!\*s) break;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;754 if (!strchr("IdvwrK", \*s))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;755 rb\_raise(rb\_eRuntimeError, "illegal switch in RUBYOPT: -%c", \*s);
> > 
> > now, given RUBYOPT=-rubygems, and \*s==r, we call moreswitches
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;756 s = moreswitches(s);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;757 }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;758 }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;759 }
> > 
> > so in this function
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;433 static char\*  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;434 moreswitches(s)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;435 char \*s;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;436 {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;437 int argc; char \*argv[3];  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;438 char \*p = s;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;439  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;440 argc = 2; argv[0] = argv[2] = 0;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;441 while (\*s && !ISSPACE(\*s))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;442 s++;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;443 argv[1] = ALLOCA\_N(char, s-p+2);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;444 argv[1][0] = '-';  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;445 strncpy(argv[1]+1, p, s-p);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;446 argv[1][s-p+1] = '\0';  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;447 proc\_options(argc, argv);  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;448 while (\*s && ISSPACE(\*s))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;449 s++;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;450 return s;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
> > --

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [25 April 2006 02:50 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/9 "2006-04-25T02:50:52Z")

</div>

And here is the modified test:

$old\_value = {}

def add\_to\_env(add\_this, env\_var\_name, env\_var\_value, split\_char=' ')  
&nbsp;&nbsp;result = env\_var\_value  
&nbsp;&nbsp;a = env\_var\_value.split(split\_char)  
&nbsp;&nbsp;if not (a.include?(add\_this) or a.include?("-#{add\_this}"))  
&nbsp;&nbsp;&nbsp;&nbsp;$old\_value[env\_var\_name] = env\_var\_value  
&nbsp;&nbsp;&nbsp;&nbsp;if a.length == 1 and a[0][0].chr != '-'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[0] = '-' + a[0]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;a \<\< "-#{add\_this}"  
&nbsp;&nbsp;&nbsp;&nbsp;result = a.join(split\_char)  
&nbsp;&nbsp;end  
&nbsp;&nbsp;result  
end

def restore\_env(env\_var\_name, env\_var\_value)  
&nbsp;&nbsp;if $old\_value[env\_var\_name]  
&nbsp;&nbsp;&nbsp;&nbsp;result = $old\_value[env\_var\_name]  
&nbsp;&nbsp;&nbsp;&nbsp;$old\_value[env\_var\_name] = nil  
&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;env\_var\_value  
&nbsp;&nbsp;end  
end

require 'test/unit'

class RubyOptTest \< Test::Unit::TestCase  
&nbsp;&nbsp;def test\_add\_to\_rubyopt  
&nbsp;&nbsp;&nbsp;&nbsp;to\_add = 'rubygems'  
&nbsp;&nbsp;&nbsp;&nbsp;env\_var = 'RUBYOPT'  
&nbsp;&nbsp;&nbsp;&nbsp;# Before and after tests  
&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['', "-#{to\_add}"],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['w', "-w -#{to\_add}"],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['-w -v -somethinglong', "-w -v -somethinglong -#{to\_add}"]  
&nbsp;&nbsp;&nbsp;&nbsp;].each do |before, after|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(after, add\_to\_env(to\_add, env\_var, before))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(before, restore\_env(env\_var, after))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;# Here nothing should be changed  
&nbsp;&nbsp;&nbsp;&nbsp;[to\_add, "-w -#{to\_add}", "-w -v -#{to\_add} -somethinglong"].each do |s|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(s, add\_to\_env(to\_add, env\_var, s))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_equal(s, restore\_env(env\_var, s))  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end  
\_\_END\_\_

Again if anything looks wrong let me know 🙂

Ryan

> **···**
>
> On 4/24/06, Ryan Leavengood \<leavengood@gmail.com\> wrote:
> 
> > OK, so not only do I need to check for an existing RUBYOPT, but if is  
> > defined and it only has one option, I need to add a dash to the  
> > existing if it lacks it, then add -rubygems. Then on uninstall this  
> > needs to be restored. Nice. I'm glad I'm prototyping this in Ruby.

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [25 April 2006 03:39 UTC](https://rubytalk.org/t/format-of-rubyopt/26917/10 "2006-04-25T03:39:24Z")

</div>

it looks that way to me. but caveat emptor.

-a

> **···**
>
> On Tue, 25 Apr 2006, Wilson Bilkovich wrote:
> 
> > To summarize, if you're going to alter RUBYOPT by appending something, you  
> > should append a space and then '-rrubygems', to be sure?
> 
> --  
> be kind whenever possible... it is always possible.  
> - h.h. the 14th dali lama
