From: Joel VanderWerf Date: 2003-06-03T10:21:43+09:00 Subject: Re: case of sub! not working Ian Macdonald wrote: > Hi, > > 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.) So I guess you have to do something like ENV['PATH'] = ENV['PATH'].sub(...)