如何从红宝石脚本中引用本地宝石? [英] How to reference a local gem from a ruby script?
问题描述
我需要从纯红宝石脚本中引用本地宝石,而无需安装宝石。在如何引用红宝石中的本地宝石的路上?,我尝试使用以下设置创建Gemfile:
I need to reference a local gem from a plain ruby script, without installing the gem. On the trail of How to refer a local gem in ruby?, i tried creating a Gemfile with the following setup:
%w(
custom_gem
another_custom_gem
).each do |dependency|
gem dependency, :path => File.expand_path("../../#{dependency}", __FILE__)
end
脚本如下:
require 'custom_gem'
CustomGem::Do.something
当我执行以下操作时:
bundle exec ruby script.rb
我得到:
script.rb:1:in `require': cannot load such file -- custom_gem (LoadError) from script.rb:1:in `<main>'
如果我忽略了则需要'custom_gem'
,我得到:
If I leave out the require 'custom_gem'
, I get:
script.rb:3:in `<main>': uninitialized constant CustomGem (NameError)
我什至尝试不使用捆绑器,而只是写 gem .. 。:path =>̣...
本身,但没有结果。是否有其他方法可以从ruby脚本中引用自定义gem,而无需在本地安装gem?
I even tried without bundler, and just writing gem ... :path =>̣ ...
in the script itself, but without results. Is there any other way of referencing custom gems from ruby scripts, without installing the gems locally?
推荐答案
请确保您的宝石名称与Gemfile中的相同(例如 custom_gem
)
Make sure that your gem name as same as in Gemfile (e.g. custom_gem
)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
别忘了使用捆绑器实际安装此宝石
Don't forget to actually install this gem using bundler
bundle install
此后,脚本应已准备就绪,可供 bundle exec ruby script.rb
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something
这篇关于如何从红宝石脚本中引用本地宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!