# Is it possible to override a 'read only' environmental variable like $"?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-override-a-read-only-environmental-variable-like/158180>\
**Category:** Ruby\
**Created:** [January 20, 2009, 6:41am UTC](https://www.ruby-forum.com/t/is-it-possible-to-override-a-read-only-environmental-variable-like/158180 "2009-01-20T06:41:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 20, 2009, 6:41am UTC](https://www.ruby-forum.com/t/is-it-possible-to-override-a-read-only-environmental-variable-like/158180/1 "2009-01-20T06:41:05Z")

</div>

Hi guys/gals,  
I am trying to write a method to give the full paths of all files  
loaded into a program when a require statement is executed.  
I would like to be able to get the same result each time a “require  
‘file’” statement is executed. To do so, I need to get the $"  
back to the state it was in prior to the require command being  
executed. But alas it is read only and does not allow me to redefine  
it.  
Anyone know how to override a read only setting or circumvent this  
somehow.  
Thanks is advance,  
Tim  
See the code below:

def catch\_new\_paths(file\_to\_require)  
old = $".dup  
require(file\_to\_require)  
new = $".dup  
after = new - old  
after.each{|file| $:.each{|path| puts path + “/” + file if  
File.exists?(path + “/” + file)}}  
#at this point I would like to convert the $" variable back to its  
state before the require statement was run with  
#$" = old  
#but doing so results in

# -:7:in `catch\_new\_paths’: $" is a read-only variable (NameError)

end

catch\_new\_paths(“base64”)

# \>\> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/

ruby/1.8/universal-darwin9.0/nkf.bundle

# \>\> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/

ruby/1.8/kconv.rb

# \>\> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/

ruby/1.8/base64.rb

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 20, 2009, 10:04am UTC](https://www.ruby-forum.com/t/is-it-possible-to-override-a-read-only-environmental-variable-like/158180/2 "2009-01-20T10:04:13Z")

</div>

timr wrote:

> I am trying to write a method to give the full paths of all files  
> loaded into a program when a require statement is executed.  
> I would like to be able to get the same result each time a “require  
> ‘file’” statement is executed. To do so, I need to get the $"  
> back to the state it was in prior to the require command being  
> executed. But alas it is read only and does not allow me to redefine  
> it.

Try Array#replace

irb(main):002:0\> $".replace([])  
=\> []  
irb(main):003:0\> $"  
=\> []

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 20, 2009, 11:50am UTC](https://www.ruby-forum.com/t/is-it-possible-to-override-a-read-only-environmental-variable-like/158180/3 "2009-01-20T11:50:41Z")

</div>

2009/1/20 timr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hi guys/gals,  
> I am trying to write a method to give the full paths of all files  
> loaded into a program when a require statement is executed.  
> I would like to be able to get the same result each time a “require  
> ‘file’” statement is executed. To do so, I need to get the $"  
> back to the state it was in prior to the require command being  
> executed. But alas it is read only and does not allow me to redefine  
> it.  
> Anyone know how to override a read only setting or circumvent this  
> somehow.

As far as I can see you do not need that. You can do this:

#!/bin/env ruby

def require\_logged file  
old = $“.dup  
if require file  
($” - old).each do |loaded\_file|  
full = nil  
$:.each do |path|  
full = File.join(path, loaded\_file)  
break nil if test(?r, full)  
end and raise “Could not find file #{loaded\_file}”  
puts “loaded #{full}”  
end  
end  
end

p require\_logged(‘socket’)  
p require\_logged(‘socket’)

Kind regards

robert
