Ruby 2.1.7 and Rails 4.0.2
while working with rails console. I have seen that If I am declaring an Object in the console. Than calls reload! and then calls a method on that same object with code that create an instance of another class will raise an error:
ArgumentError: A copy of First has been removed from the module tree but is still active!
An example is much easier to understand:
In my app/model folders I declare to class:
app/models/first.rb
class First
def foo
Second.new
end
end
app/models/second.rb
class Second
end
Than in the console I ran this commands:
x = First.new
reload!
x.foo
Than I get the exception
Can you give an explanation why is this happening? Is it mistake to use a reference that was declared before the reload? Is the exception is a good thing and I should not try to find a workaround or it shows that something in my code or configuration is wrong
I have this question: A copy of xxx has been removed from the module tree but is still active
One solution was to add extra :: as a prefix and indeed if I use this:
::Second.new
I do not get the exception
Do you think that using this solution is a good thing or is too much hacky and not the Ruby Way?
Can you explain why this does not cause the error?