[英]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. 在我的Web主机的系统上,安装和使用gems似乎需要安装我自己的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.
我正在尝试在Rails应用程序中使用特定的宝石(称为图像科学)。
At this point, if I open irb
and type the following, this is what I get: 在这一点上,如果我打开
irb
并输入以下内容,这就是我得到的:
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: 根据主持人的建议,我回到bash并输入以下内容:
export LD_LIBRARY_PATH=~/ruby1.8/lib
That command allows irb to require image_science
- it returns 'true.' 该命令允许irb要求
image_science
它返回“ 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. 问题在于这不会改变我的Ruby脚本可以访问的内容。 (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. (它也仅在会话中持续存在,但是我想可以将其添加到
.bashrc
。)在我的Rails应用程序中,如果尝试使用该gem仍然会出错。
So I've got two questions : 所以我有两个问题 :
If the gem is only going to be used by one model, I generally just do require 'gem' on that model. 如果只有一个模型可以使用gem,那么我通常只需要在该模型上使用'gem'。 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.
如果应用程序要使用gem,例如在视图或控制器中,我将创建一个名为app.rb的文件,并将其粘贴在包含所有require语句的config / initializers中。 You can also include it in config/environment.rb, inside the initializer block:
您还可以在初始化程序块内的config / environment.rb中包含它:
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) 对于LD_LIBRARY_PATH,请将其放入config / environments / *。rb文件之一(根据您的环境进行定制,但是开发很可能与生产环境不同)
ENV['LD_LIBRARY_PATH'] = "#{ENV['LD_LIBRARY_PATH']}:#{ENV['JAVA_HOME']}/jre/lib/amd64:#{ENV['JAVA_HOME']}/jre/lib/amd64/server"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.