On Tue 03 Jun 2003 at 10:21:43 +0900, Joel VanderWerf wrote:

> Ian Macdonald wrote:
> >
> >Here's an interesting phenomenon:
> >
> >[ianmacd / baghdad]$ irb
> >irb(main):001:0> ENV['PATH']
> >=> "/usr/bin:/bin:/usr/X11R6/bin:/usr/pubsw/bin:/usr/kerberos/bin"
> >irb(main):002:0> ENV['PATH'].sub!(%r(:/usr/kerberos/bin), '')
> >=> "/usr/bin:/bin:/usr/X11R6/bin:/usr/pubsw/bin"
> >irb(main):003:0> ENV['PATH']
> >=> "/usr/bin:/bin:/usr/X11R6/bin:/usr/pubsw/bin:/usr/kerberos/bin"
> >irb(main):004:0> puts VERSION
> >1.8.0
> >=> nil
> >
> >Why doesn't the sub! work in line 002? It returns the correct answer,
> >but ENV['PATH'] is not modified in-place. I've verified that the
> >behaviour is the same in 1.6, too.
> 
> ENV is not exactly a Hash, but a singleton that behaves kinda like one. 
> Apparently, one difference is that ENV[key] returns a copy of the value. 
> (In hash.c, you can verify this by seeing that rb_f_getenv() calls 
> rb_str_new2 or rb_tainted_str_new2 to generate the return value.)

Thanks for the explanation. This definitely violates the principle of
least surprise, however, and I consider it a bug. At the very least, it
should go into the documentation, including the FAQ.

> So I guess you have to do something like
> 
>   ENV['PATH'] = ENV['PATH'].sub(...)

Yes, that's what I came up with, too.

Ian
-- 
Ian Macdonald               | Harrisberger's Fourth Law of the Lab: 
System Administrator        | Experience is directly proportional to the 
ian / caliban.org             | amount of equipment ruined. 
http://www.caliban.org      | 
                            |