0

我将slimgem 安装到我自己的自定义目录中,但 require 'slim' 给了我错误。

安装苗条:

$ cd /var/www
$ mkdir project
$ cd project/
$ gem install slim -i gems/
Fetching: temple-0.4.0.gem (100%)
Fetching: slim-1.2.2.gem (100%)
Successfully installed temple-0.4.0
Successfully installed slim-1.2.2
2 gems installed
Installing ri documentation for temple-0.4.0...
Installing ri documentation for slim-1.2.2...
Installing RDoc documentation for temple-0.4.0...
Installing RDoc documentation for slim-1.2.2...
$ ls gems/
bin/            doc/            specifications/ 
cache/          gems/           
$ ls gems/gems/
slim-1.2.2  temple-0.4.0
$ ls gems/gems/slim-1.2.2/
benchmarks  CHANGES  lib      Rakefile   slim.gemspec
bin         Gemfile  LICENSE  README.md  test
$ ls gems/gems/slim-1.2.2/lib/
slim  slim.rb

Gem 环境(如果你注意到我添加/var/www/project/gems到 GEM_PATH):

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.11
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.9.1
     - /home/john/.gem/ruby/1.9.1
     - /var/www/project/gems
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gempath" => ["/usr/local/lib/ruby/gems/1.9.1", "/home/john/.gem/ruby/1.9.1", "/var/www/project/gems"]
  - REMOTE SOURCES:
     - http://rubygems.org/

测试:

$ irb
irb(main):001:0> require 'slim'
LoadError: no such file to load -- slim
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from <internal:lib/rubygems/custom_require>:29:in `require'
    from (irb):1
    from /usr/local/bin/irb:12:in `<main>'

知道有什么问题吗?我/var/www/project/gems在 GEM_PATH 中添加了但require 'slim'不会加载 gem,为什么会这样?这是 ruby​​gems 的错误吗?

当我写和时同样require 'slim'/var/www/project/index.rb问题ruby index.rb

4

2 回答 2

3

我衷心推荐使用bundler,它不仅可以管理这个,还可以代表您进行版本固定和部署。

相关的调用将是;在Gemfile

source :rubygems
gem "slim"

进而

gem install bundler && bundle install && bundle exec irb

届时require 'slim'将起作用。

于 2012-06-24T10:16:48.520 回答
1

由于您正在安装 slim,/var/www/project/gems/您必须将其添加到加载路径或通过完整路径要求它:

require '/var/www/project/gems/slim'

或将其添加到您的加载路径:

$LOAD_PATH.unshift(File.dirname('/var/www/project/gems/'))
于 2012-06-24T08:48:01.637 回答