如何使 ruby​​ gem 可用于我的脚本?

发布于 2024-08-23 22:45:27 字数 840 浏览 3 评论 0原文

在我的网络主机系统上,安装和使用 gems 似乎需要安装我自己的 ruby​​ 副本。在他们在论坛上的帮助下,我已经完成了。我正在尝试在 Rails 应用程序中使用特定的 gem(称为 Image Science)。

此时,如果我打开 irb 并输入以下内容,这就是我得到的结果:

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

根据主机的建议,我返回 bash 并输入以下内容:

export LD_LIBRARY_PATH=~/ruby1.8/lib

That command allowed irb to require image_science - 它返回“true”。据我了解,它的意思是,“嘿,也在这个目录中查找宝石。”

问题是这不会改变我的 Ruby 脚本可以访问的内容。 (它也只在会话中持续存在,但我想我可以将它添加到我的 .bashrc 中。)在我的 Rails 应用程序中,如果我尝试要求该 gem,我仍然会收到错误。

所以我有两个问题

  1. 如何使这个 gem 可用于我的 Ruby 脚本 - 特别是 Rails 模型文件?
  2. 我是否需要将“require”命令放入模型文件中,或者是否有其他方式加载gem?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-08-30 22:45:27

如果宝石仅由一个模型使用,我通常只需要该模型上的“宝石”。如果应用程序要使用 gem,比如在视图或控制器中,我会创建一个名为 app.rb 的文件,并将其粘贴到包含所有 require 语句的 config/initializers 中。您还可以将其包含在 config/environment.rb 的初始化程序块内:

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

这将在项目加载之前需要该 gem,但是,我在使用某些 gem(例如 ruby​​ 方面)时遇到了麻烦。

对于 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"

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文