简体   繁体   English

如何使红宝石宝石可用于脚本?

[英]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 : 所以我有两个问题

  1. How do I make this gem available to my Ruby scripts - specifically, a Rails model file? 如何使该gem对我的Ruby脚本可用-特别是Rails模型文件?
  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? 我是否应该甚至在模型文件中放入“ require”命令,还是应该以其他方式加载gem?

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.

相关问题 如何以线程安全的方式将会话数据提供给我的ruby gem? - How can I make session data available to my ruby gem in a thread safe way? 如何使gem中的功能可用于Sinatra视图? - How do I make functions in a gem available to Sinatra views? 如何使我现有的ruby gem配置可用于rails-api应用程序? - How to make my existing ruby gem configuration available to my rails-api app? 如何在Rails网站上使用Ruby gem? - How do I use a Ruby gem on my Rails site? Ruby gem方法只在控制器中可用? 如果是这样,如何在其他文件中使用它? - Ruby gem method only available in controller? If so, how to make it available in other files? 如何使用Rubber gem运行其他脚本? - How do I run additional scripts using the Rubber gem? 如何使lib模块方法在项目范围内可用,并且还特定于Ruby on Rails中的类? - How do I make a lib module method available project-wide and also specific to a class in Ruby on Rails? 如何使模块方法可用于常量 Proc 变量? (红宝石) - How do I make module methods available to a constant Proc variable? (Ruby) 如果修复了错误,如何使用Ruby gem? - How do I use a Ruby gem if I fixed a bug? 如何使用ruby-debug gem调试插件或gem,我要调试的部分从测试脚本开始? - How to debug a plugin or gem using ruby-debug gem, where the part I wanted to debug started from test scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM