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


#1

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


#2

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> $"
=> []


#3

2009/1/20 timr 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