开发者

How do I make a ruby gem available to my scripts?

On my web host's system, installing and using gems seems to require installing my own copy of ruby. With their help on a forum, I've gotten that done. I'm trying to use a particular gem (called Image Science) in a Rails app.

At this point, if I open irb and type the following, this is what I get:

require 'rubygems' #true
require 'image_science' #LoadError: libfreeimage.so.3: cannot open shared 
                         #object file: No such file or directory (etc)....

On the host's advice, I go back to bash and type 开发者_运维技巧this:

export LD_LIBRARY_PATH=~/ruby1.8/lib

That command allows irb to require image_science - it returns 'true.' As I understand it, it's saying, "hey, also look in this directory for gems."

The problem is that this doesn't change what my Ruby scripts can access. (It also only persists for the session, but I suppose I can add it to my .bashrc.) In my Rails app, I still get an error if I try to require that gem.

So I've got two questions:

  1. How do I make this gem available to my Ruby scripts - specifically, a Rails model file?
  2. Should I even need to put the "require" command in the model file, or is there some other way that the gem should get loaded?


If the gem is only going to be used by one model, I generally just do require 'gem' on that model. If the app is going to use the gem, say in the view or a controller, I create a file called app.rb and stick it in config/initializers that includes all the require statements. You can also include it in config/environment.rb, inside the initializer block:

config.gem 'pg', :lib => 'pg'

which will require that gem before the project loads, however, I've had trouble with that with certain gems, like ruby facets.

For the LD_LIBRARY_PATH, put this in one of the config/environments/*.rb files (customize for your environments, but development is most likely different from production)

ENV['LD_LIBRARY_PATH'] = "#{ENV['LD_LIBRARY_PATH']}:#{ENV['JAVA_HOME']}/jre/lib/amd64:#{ENV['JAVA_HOME']}/jre/lib/amd64/server"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜