Matthew Thill
2005-Mar-24 21:56 UTC
load, require, webrick and dynamically reloading changes
I''m seeing some strange behaviour and I''m hoping someone can
educate me
on why things are happening the way they are.
I have the following controller:
load ''some_object.rb''
class SomethingController < ApplicationController
def index
so = SomeObject.new(5,10)
render_text ''success''
end
end
and the following class in the file lib/some_object.rb:
class SomeObject
attr_reader :value1, :value2
end
I start webrick and go to the following url (replace ..... with where
webrick is listening) http://........./something/ and get an error -
"wrong number of arguments (2 for 0)". This is expected.
I modify lib/some_object.rb to this:
class SomeObject
attr_reader :value1, :value2
def initialize(v1,v2)
@value1 = v1
@value2 = v2
end
end
I reload the page and get the expected result, the word "success" on
the
page.
Now the strange thing. If I go back to some_object.rb and delete the
initialize method, so I''m back to the original version, and reload the
page again, everything works!! It shouldn''t, I should get that
"wrong
number of arguments (2 for 0)" error, but I don''t. Just the word
"success" staring back at me. So I stop and restart webrick, then
reload
the page once more. This time, I get the error.
Why does webrick reload the class with the first change, but not the
second? It seems as if once a version of the class SomeObject with an
initialize method expecting 2 parameters has been loaded, this is always
available, even if the actual class file has changed. I thought the load
method would reload the class each time.
Does anyone have any insight?
Nicholas Seckar
2005-Mar-25 15:56 UTC
Re: load, require, webrick and dynamically reloading changes
On Thursday 24 March 2005 16:56, Matthew Thill wrote:> class SomeObject > attr_reader :value1, :value2 > end > > class SomeObject > attr_reader :value1, :value2 > def initialize(v1,v2) > @value1 = v1 > @value2 = v2 > end > end > > I should get that "wrong number of arguments (2 for 0)" error, but I don''t.Actually, you shouldn''t. First, you declare a class. Then, on the next request, you added initialize to it, and you tried to redefine the accessors. Then, on the request after that, you redefined the accessors. You haven''t cleared the class -- it''s been the same object all along, and so it has been accumulating methods all along. You could add Object.send(:const_remove, :SomeObject) if const_defined?(:SomeObject) But you should be using Rail''s require_dependency instead of load. -- Nicholas Seckar aka. Ulysses