I have a module that lives in my Rails app as lib/render_graphs.rb. It looks like:
require 'gnuplot'
module RenderGraphs
def render_standard_curve_graph(standard_curve, params, term = nil, output = nil)
Gnuplot.open do |gp|
# do things
end
end
end
if __FILE__ == $0
include RenderGraphs
render_standard_curve_graph(...)
end
If I invoke it on the command line with ruby -r rubygems render_graphs.rb, it works just fine. But if I try to call render_standard_curve_graph from my Rails app (I call it from a controller, which includes RenderGraphs) or from the Rails console, it gives an error like:
NameError: uninitialized constant RenderGraphs::Gnuplot
from .../lib/render_graphs.rb:31:in `render_standard_curve_graph'
from (irb):32
If I run require '/Library/Ruby/Gems/1.8/gems/gnuplot-2.3.6/lib/gnuplot.rb' at the Rails console before I call render_standard_curve_graph, it works just fine. What’s wrong with gem 'gnuplot' in my Gemfile? bundle show gnuplot yields
/Library/Ruby/Gems/1.8/gems/gnuplot-2.3.6…
I am baffled. gnuplot is in my Gemfile and I’ve run bundle install and restarted the console (several times). Similar calls to other gems (GSL and roo) work just fine from other modules in my lib directory. What am I missing?
It’s a bug in the GSL gem. The GSL gem must be loaded after the gnuplot gem, or else it prevents the Gnuplot module from appearing to the interpreter. Bundler does not support requiring gems in a particular order, so the fix that does not require modifying the gsl gem is to edit config/boot.rb and explicitly
require 'gnuplot'immediately afterrequire 'rubygems', before Bundler is initialized.