我正在开发一个程序,该程序有助于在命令行上(在https://github.com/keithrbennett/rexe,Y = np.matrix(logdif)
上)运行Ruby代码。
当我gem install rexe
并cd到项目根目录,并尝试gem install
不在Gemfile中的文件时,它失败:
require
但是,当我➜ rexe git:(master) ✗ rexe -r awesome_print -oa '"hello"'
Traceback (most recent call last):
12: from /Users/kbennett/.rvm/gems/ruby-2.6.0/bin/ruby_executable_hooks:24:in `<main>'
11: from /Users/kbennett/.rvm/gems/ruby-2.6.0/bin/ruby_executable_hooks:24:in `eval'
10: from /Users/kbennett/.rvm/gems/ruby-2.6.0/bin/rexe:23:in `<main>'
9: from /Users/kbennett/.rvm/gems/ruby-2.6.0/bin/rexe:23:in `load'
8: from /Users/kbennett/work/rexe/exe/rexe:403:in `<top (required)>'
7: from /Users/kbennett/.rvm/gems/ruby-2.6.0/gems/bundler-2.0.1/lib/bundler.rb:313:in `with_clean_env'
6: from /Users/kbennett/.rvm/gems/ruby-2.6.0/gems/bundler-2.0.1/lib/bundler.rb:562:in `with_env'
5: from /Users/kbennett/.rvm/gems/ruby-2.6.0/gems/bundler-2.0.1/lib/bundler.rb:313:in `block in with_clean_env'
4: from /Users/kbennett/work/rexe/exe/rexe:403:in `block in <top (required)>'
3: from /Users/kbennett/work/rexe/exe/rexe:385:in `call'
2: from /Users/kbennett/work/rexe/exe/rexe:385:in `each'
1: from /Users/kbennett/work/rexe/exe/rexe:385:in `block in call'
/Users/kbennett/work/rexe/exe/rexe:385:in `require': cannot load such file -- awesome_print (LoadError)
时,它工作正常:
cd ~
我将代码包装在➜ ~ rexe -r awesome_print -oa '"hello"'
"hello"%
中,我认为应该解决这个问题。
我可以直接使用Ruby解释器,并且可以:
Bundler.with_clean_env
因此,我想Bundler做出了一个假设,即不允许加载非Gemfile列出的gem,这几乎总是合理的,但就我而言,程序应该能够加载任何gem,这是一个障碍。
我知道Bundler正在查找Gemfile [.lock]并执行其认为正确的操作,但是有什么解决办法吗?
答案 0 :(得分:1)
不是一个完整的答案,只是一个方向:尝试在需要gem之前,将请求的gem的路径显式添加到$LOAD_PATH
中。例如:
pry(main)> require "awesome_print"
LoadError: cannot load such file -- awesome_print
但是然后
pry(main)> $LOAD_PATH << "<home>/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/awesome_print-1.8.0/lib/";
pry(main)> require "awesome_print"
=> true
Voila!问题是如何为宝石找到正确的路径,也许gem which
可以帮助...