我需要在我的项目中使用两个宝石,它们都声称拥有PDF命名空间:pdf-reader和htmldoc。
有没有办法让它们一起很好地运行?我能想到的唯一方法是重写自己的版本htmldoc,以给它一个不同的命名空间。
我需要在我的项目中使用两个宝石,它们都声称拥有PDF命名空间:pdf-reader和htmldoc。
有没有办法让它们一起很好地运行?我能想到的唯一方法是重写自己的版本htmldoc,以给它一个不同的命名空间。
ObjectSpace
,对吗? - Andrew GrimmObjectSpace.each_object(Module)
,尽管它没有完整实现ObjectSpace
。 - Jörg W Mittag这个问题可能没有优雅的解决方案。如果您确实需要这两个宝石并存,我认为您最好的选择是fork其中之一(或可能都要fork)并使用您的fork。以下是我的做法:
.gemspec
file in the root of the repository, the next step will not work otherwise.Use Bundler to manage your projects dependencies. Instead of specifying the dependency to the library you've modified as
gem 'the_gem'
specify it like this:
gem 'the_gem', :git => 'git://github.com/you/the_gem.git'
(but change the URL to the repository to the actual one)
Send an e-mail to the maintainer of the gem you modified and ask him or her to consider merging in your changes in the next release.
我听说过一种新的功能叫做“细化”。它旨在避免两个不同的猴子补丁影响同一个类而导致问题,但我想看看它是否能帮助解决你的问题。
我在链接中回答了这个问题 https://stackoverflow.com/a/37311072/292780
对上面的答案表示不同意。以下是我的做法:
ruby -S gem list my_gem
`*** LOCAL GEMS ***
my_gem (1.0.1, 1.0.0, 0.0.2)
`
ruby -S gem lock my_gem-1.0.0 > locklist.rb
这个命令会生成一个特定版本的依赖列表到locklist文件中。
require 'rubygems'
gem 'my_gem', '= 1.0.0'
gem 'gem_base', '= 1.0.0'
gem 'rest-client', '= 1.7.2'
gem 'savon', '= 1.1.0'
gem 'addressable', '= 2.3.6'
gem 'mime-types', '= 1.25.1'
gem 'netrc', '= 0.11.0'
load('locklist.rb')
,它会加载一个特定版本的gem和它所依赖的库。瞧,没有Bundler了。