where ‘require’ 和有什么不一样?看起来以及“Gem.available”在哪里?寻找宝石?
我对路径如何与宝石一起使用有点困惑。这是我困惑的核心:
>> require 'rubygems'
=> false
>> Gem.available?('money')
=> true
>> require 'money'
LoadError: no such file to load -- money
from (irb):3:in `require'
from (irb):3
from /usr/bin/irb:12:in `<main>'
>> Gem.available?('pony')
=> false
>> require 'pony'
=> true
这是由 irb 0.9.5(05/04/13)
运行的,这特别令人困惑,因为 gem list --local
产生
*** LOCAL GEMS ***
albino (1.0)
bcat (0.5.2)
builder (2.1.2)
bundler (1.0.0)
classifier (1.3.3)
diff-lcs (1.1.2)
directory_watcher (1.3.2)
fast-stemmer (1.0.0)
gdata (1.1.1)
gdata2 (0.1)
github-markup (0.5.0)
gollum (1.0.1)
google4r-checkout (1.0.6.1)
grit (2.2.0)
jekyll (0.7.0)
kwalify (0.7.1)
liquid (2.2.2)
maruku (0.6.0)
mime-types (1.16)
mocha (0.9.8)
money (3.0.5)
mustache (0.11.2)
nokogiri (1.4.3.1)
rack (1.2.1, 1.1.0)
rake (0.8.7)
rubygems-update (1.3.7)
sanitize (1.2.1)
sinatra (1.0)
syntax (1.0.0)
unicorn (1.1.3)
:值得注意的是包含金钱
但不小马
。
以下是 gem env 的输出:
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.1 (2010-01-10 patchlevel 378) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/bin/ruby1.9.1
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.9.1
- /home/aresnick/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
如果没有明确说明,gem 和 ruby 使用相同的 ruby,即:
aresnick@dror:~$ which ruby
/usr/bin/ruby
aresnick@dror:~$ ls -al $(which ruby)
lrwxrwxrwx 1 root root 22 2010-08-28 20:08 /usr/bin/ruby -> /etc/alternatives/ruby
aresnick@dror:~$ ls -al /etc/alternatives/ruby
lrwxrwxrwx 1 root root 18 2010-08-28 20:08 /etc/alternatives/ruby -> /usr/bin/ruby1
我怀疑问题出在正在查看的路径上,特别是因为我可以访问和无法访问的其他宝石,例如 gdata
不可用并且 sinatra
是。 。 。
aresnick@dror:~$ gem which pony
/var/lib/gems/1.9.1/gems/pony-1.0/lib/pony.rb
aresnick@dror:~$ gem which money
/usr/lib/ruby/gems/1.9.1/gems/money-3.0.5/lib/money.rb
aresnick@dror:~$ gem which sinatra
/var/lib/gems/1.9.1/gems/sinatra-1.0/lib/sinatra.rb
aresnick@dror:~$ gem which gdata
/usr/lib/ruby/gems/1.9.1/gems/gdata-1.1.1/lib/gdata.rb
aresnick@dror:~$ irb
>> require 'sinatra'
=> true
>> require 'gdata'
LoadError: no such file to load -- gdata
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
所以,我认为 /usr/lib/ruby/gems/1.9.1
gem 没有被发现,而 /var
却被发现了。我确信使用符号链接可以解决这个问题,但我想知道a)为什么会发生这种情况,b)这些路径在哪里设置以及如何设置它们?
I'm a bit confused about how paths work with gems. Here's the core of my confusion:
>> require 'rubygems'
=> false
>> Gem.available?('money')
=> true
>> require 'money'
LoadError: no such file to load -- money
from (irb):3:in `require'
from (irb):3
from /usr/bin/irb:12:in `<main>'
>> Gem.available?('pony')
=> false
>> require 'pony'
=> true
This is being run by irb 0.9.5(05/04/13)
This is especially confusing because gem list --local
yields:
*** LOCAL GEMS ***
albino (1.0)
bcat (0.5.2)
builder (2.1.2)
bundler (1.0.0)
classifier (1.3.3)
diff-lcs (1.1.2)
directory_watcher (1.3.2)
fast-stemmer (1.0.0)
gdata (1.1.1)
gdata2 (0.1)
github-markup (0.5.0)
gollum (1.0.1)
google4r-checkout (1.0.6.1)
grit (2.2.0)
jekyll (0.7.0)
kwalify (0.7.1)
liquid (2.2.2)
maruku (0.6.0)
mime-types (1.16)
mocha (0.9.8)
money (3.0.5)
mustache (0.11.2)
nokogiri (1.4.3.1)
rack (1.2.1, 1.1.0)
rake (0.8.7)
rubygems-update (1.3.7)
sanitize (1.2.1)
sinatra (1.0)
syntax (1.0.0)
unicorn (1.1.3)
which notably contains money
but not pony
.
Here's the outputs of gem env
:
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.1 (2010-01-10 patchlevel 378) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/bin/ruby1.9.1
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.9.1
- /home/aresnick/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
And in case that doesn't make it clear, gem
and ruby
are using the same ruby, i.e.:
aresnick@dror:~$ which ruby
/usr/bin/ruby
aresnick@dror:~$ ls -al $(which ruby)
lrwxrwxrwx 1 root root 22 2010-08-28 20:08 /usr/bin/ruby -> /etc/alternatives/ruby
aresnick@dror:~$ ls -al /etc/alternatives/ruby
lrwxrwxrwx 1 root root 18 2010-08-28 20:08 /etc/alternatives/ruby -> /usr/bin/ruby1
I suspect the problem is with the paths that are being looked at, especially because of the other gems which I can and can't access, e.g. gdata
is not available and sinatra
is. . .
aresnick@dror:~$ gem which pony
/var/lib/gems/1.9.1/gems/pony-1.0/lib/pony.rb
aresnick@dror:~$ gem which money
/usr/lib/ruby/gems/1.9.1/gems/money-3.0.5/lib/money.rb
aresnick@dror:~$ gem which sinatra
/var/lib/gems/1.9.1/gems/sinatra-1.0/lib/sinatra.rb
aresnick@dror:~$ gem which gdata
/usr/lib/ruby/gems/1.9.1/gems/gdata-1.1.1/lib/gdata.rb
aresnick@dror:~$ irb
>> require 'sinatra'
=> true
>> require 'gdata'
LoadError: no such file to load -- gdata
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
So, I think that the /usr/lib/ruby/gems/1.9.1
gems are not being found and the /var
ones are. I'm sure mucking around with symlinks could fix this, but I was wondering a) why this was happening and b) where are these paths are set and how can I set them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 Debian 系统上遇到了类似的问题,我猜你正在使用该系统,或者是基于它的系统,因为你正在使用替代方案,然后问题是我通过 apt-get 安装了 rubygems,然后安装了通过源更新的版本。
把事情搞砸了就好。
Debian 和 rubygems 团队之间存在某种“宗教”战争,因此在 Debian 上从源代码安装 rubygems 更容易。
但我对发生的事情没有任何解释。 :(
I've had similar problems on a Debian system, which I'm guessing you're using or a system based off of since you're using alternatives, and the problem then was that I had installed rubygems through apt-get and then installed an updated version through source.
Messed things up good.
There is some kind of "religious" war between the Debian and rubygems team so it's just easier to installed rubygems from source on Debian.
I've no explanation for what is happening though. :(