Forum: Ruby is it possible to override a 'read only' environmental variable like $" ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
699a3d471442eb22c0ab9458c2c573a4?d=identicon&s=25 timr (Guest)
on 2009-01-20 06:41
(Received via mailing list)
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-20 10:04
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> $"
=> []
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-20 11:50
(Received via mailing list)
2009/1/20 timr <timrandg@gmail.com>:
> 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
This topic is locked and can not be replied to.