tags:

views:

47

answers:

1

I have the following in the file a.rb:

require foo

and i need to unload foo, to load the foo from b.rb, c.rb and other files.

How i can do?

A: 
Object.send(:remove_const, :Foo)

assuming your class is named Foo.

lwe
This works only one time..
tapioco123
irb(main):002:0> require 'timeout'=> trueirb(main):004:0> Object.send(:remove_const, :Timeout)=> Timeoutirb(main):005:0> require 'timeout'=> false
tapioco123
mhh, yes, because require checks if it has already loaded that lib, maybe you need to use `load` instead of `require`... why do you need to unload that const anyway? can't you use modules or anything to shield them?PS: you need to append the file extension in `load`
lwe
but aren't there multiple files, so this shouldn't be a problem at all? so shouldn't some hacky magic like `%w{a b c d}.each { |f| require(f); Foo.bar; Object.send(:remove_const, :Foo) }` work "as expected" in your case - though I don't really recommend it. Just seems strange, having all these .rb files defining the same class + method.
lwe
^^ it works, `a.rb`, `b.rb`, `c.rb` and `d.rb` look like: `class Foo; def self.bar; puts __FILE__ end end` and then I have the main loop as described above in a file named `prg.rb`... and the output is, as expected :) => `./a.rb ./b.rb ./c.rb ./d.rb`
lwe
Sorry but for me after the first cycle defined?(Foo) returns false
tapioco123
%w{a b c d}.each { |f| require(f); Foo.bar if defined?(Foo); Object.send(:remove_const, :Foo) }
tapioco123