From: Ryan Leavengood Date: 2006-04-25T11:50:52+09:00 Subject: Re: Format of RUBYOPT On 4/24/06, Ryan Leavengood 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. And here is the modified test: $old_value = {} def add_to_env(add_this, env_var_name, env_var_value, split_char=' ') result = env_var_value a = env_var_value.split(split_char) if not (a.include?(add_this) or a.include?("-#{add_this}")) $old_value[env_var_name] = env_var_value if a.length == 1 and a[0][0].chr != '-' a[0] = '-' + a[0] end a << "-#{add_this}" result = a.join(split_char) end result end def restore_env(env_var_name, env_var_value) if $old_value[env_var_name] result = $old_value[env_var_name] $old_value[env_var_name] = nil result else env_var_value end end require 'test/unit' class RubyOptTest < Test::Unit::TestCase def test_add_to_rubyopt to_add = 'rubygems' env_var = 'RUBYOPT' # Before and after tests [ ['', "-#{to_add}"], ['w', "-w -#{to_add}"], ['-w -v -somethinglong', "-w -v -somethinglong -#{to_add}"] ].each do |before, after| assert_equal(after, add_to_env(to_add, env_var, before)) assert_equal(before, restore_env(env_var, after)) end # Here nothing should be changed [to_add, "-w -#{to_add}", "-w -v -#{to_add} -somethinglong"].each do |s| assert_equal(s, add_to_env(to_add, env_var, s)) assert_equal(s, restore_env(env_var, s)) end end end __END__ Again if anything looks wrong let me know :) Ryan