This seems very strange to me but I have a script that I’m running that is somehow keeping previous values from previous loads of the script. The values in req1 aren’t being dumped on successive loads of the script.
Here’s the script:
require 'amazon_product'
req1 = nil
req1 = AmazonProduct["us"]
puts req1.inspect
req1.configure do |c|
c.key = "[...]"
c.secret = "[...]"
c.tag = "[...]"
end
puts req1.inspect
req1 << {
'Version' => '2010-11-01',
'Operation' => 'ItemLookup',
'SearchIndex' => "Books",
'Keywords' => 'Corrections'
#, 'A' => 'B'
}
puts req1.inspect
resp = req1.get
File.open( File.join(File.dirname(__FILE__),
"amazon_response.xml"), 'w') {|f| f.write(resp.body) }
Here are the steps:
- open irb (via rails c)
- run the script via the load command
- uncomment the A=>B line
- run script again
- comment the A=>B line
- run script again
At the end the req1 object will have 'A' => 'B' in it. I’m unclear on why this would be happening since it looks like I’m creating a new object on every run on of the script. It appears that the object is being remembered on every run.
What’s the best practice to prevent this sort of thing? I’ve tried a few things but feel like I’m grasping at straws since I don’t understand why it’s happening in the first place.
You’re not restarting irb, so it depends on what
AmazonProduct[]does. From its source:It’s caching, creating a new request iff one doesn’t exist yet.
From one standpoint, it’s “because it doesn’t have a
new“.newis only called when the locale hasn’t been loaded yet. From another, it’s lessnew/not-new, but caching, without documenting it may do so.Given the behavior, it’s a reasonable assumption–and why my first thought was
[]‘s implementation.Regarding not restarting
irb: if there isn’t a mechanism to reload the cache (I didn’t check), quickest thing to do would be to monkey-patch[]and always retrieve a newRequestfor a given locale.