ruby“require”如何真正起作用?

时间:2013-07-23 02:51:52

标签: ruby ffi g-wan

我正在使用Archlinux,ruby位置在/ usr / bin / ruby​​

$ ruby --version 
ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]
$ gem install ffi
Building native extensions.  This could take a while...
Successfully installed ffi-1.9.0
Parsing documentation for ffi-1.9.0
Done installing documentation for ffi after 41 seconds
1 gem installed

我使用G-WAN执行ruby脚本,但是当我使用require 'ffi'时显示的输出结果为:

/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- ffi (LoadError)
    from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /home/asd/bin/gwan_linux32-bit/127.0.0.1_8081/#127.0.0.1/csp/test.rb:1:in `<main>'

require '/home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib/ffi.rb'显示:

/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- ffi_c (LoadError)
    from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib/ffi.rb:14:in `rescue in <top (required)>'
    from /home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib/ffi.rb:3:in `<top (required)>'
    from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
    from /home/asd/bin/gwan_linux32-bit/127.0.0.1_8081/#127.0.0.1/csp/test.rb:21:in `<main>'

p $LOAD_PATH的输出是:

["/usr/lib/ruby/site_ruby/2.0.0" # doesn't exists
, "/usr/lib/ruby/site_ruby/2.0.0/i686-linux" # doesn't exists
, "/usr/lib/ruby/site_ruby" # doesn't exists
, "/usr/lib/ruby/vendor_ruby/2.0.0" # doesn't exists
, "/usr/lib/ruby/vendor_ruby/2.0.0/i686-linux" # doesn't exists
, "/usr/lib/ruby/vendor_ruby" # doesn't exists
, "/usr/lib/ruby/2.0.0" 
, "/usr/lib/ruby/2.0.0/i686-linux"
]

FFI的位置是:

$ locate ffi.rb
/home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib/ffi.rb
/home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib/ffi/ffi.rb
/opt/jruby/lib/ruby/shared/ffi.rb
/opt/jruby/lib/ruby/shared/ffi/ffi.rb
/opt/jruby/samples/ffi/ffi.rb

所以我试着将目录添加到$ LOAD_PATH

$LOAD_PATH.unshift '/home/asd/.gem/ruby/2.0.0/gems/ffi-1.9.0/lib'

现在require 'ffi'显示没有错误..

如果我已经在〜/ .gem / ruby​​ / 2.0.0 / gems上安装了很多宝石

$ ll ~/.gem/ruby/2.0.0/gems/ | wc
285    2558   17502

使用gem的正确方法是什么?是通过添加每个路径到$ LOAD_PATH

1 个答案:

答案 0 :(得分:0)

这显然是一个Ruby配置问题,建议的RVM工具似乎解决了这个问题:

  

RVM是一个命令行工具,可让您轻松安装,管理,   并使用从解释器到集合的多个ruby环境   宝石。

正如'icktoofay'所建议的,G-WAN可以增加对RVM的支持,但我不确定它是否有意义,除非RVM变得比Ruby本身更为主流(或被Ruby同化)。

现在,Jesly Varghese还告诉我们(上图)问题来自“你正在使用系统ruby,并在用户目录中调用gem”

您是否尝试在/opt/gwan下安装G-WAN? 然后,这个PATH问题可能会消失。