0

I'm using "therubyracer" in a model, and I'm requiring at the top of the model as so:

require 'v8'
class Thing
  def self.ctx; @@ctx ||= V8::Context.new; end;

  def self.eval(script); ctx.eval(script); end;
end

However, I intermittently get:

NameError - uninitialized constant Thing::V8: 
/app/thing.rb:3:in `ctx'

When testing requests through a local Padrino server, apparently after I modify code in Thing. This is corrected by restarting the padrino server. I'm assuming requiring v8 somewhere else would fix this problem, wheres the correct place?

matt
  • 78,533
  • 8
  • 163
  • 197
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
  • 1
    I'd say you've put the `require` in the right place - near the code that needs it. How are you running the tests? – ian Jan 28 '13 at 18:32
  • Hitting the model from a controller action via POST. So padrino server is running via padrino s. Unit tests all work fine, never triggers the error. – Allyl Isocyanate Jan 28 '13 at 18:48
  • I'd add a `warn` statement in the file so you know when the file has been reloaded. – ian Jan 29 '13 at 10:13
  • , as it'll help you track down what is happening. – ian Jan 29 '13 at 10:24

2 Answers2

2

This looks like it might be caused by the Padrino reloader getting confused when it reloads your thing.rb file, causing Ruby to look for V8 in the Thing namespace.

Try explicitly specifying V8 is in the top level using the :: prefix:

def self.ctx; @@ctx ||= ::V8::Context.new; end;
matt
  • 78,533
  • 8
  • 163
  • 197
0

You can put it wherever you want if you add it on the Gemfile. Did you added it?

Thanks!

DAddYE
  • 1,719
  • 11
  • 16