问题描述
我是红宝石的新手,但是我正在开发我的第一个红宝石程序.它目前有两个文件,一个是函数库(xgync.rb
存储在lib
中),另一个是可执行文件xgync
存储在"bin"中. (在此处 https://bitbucket.org/jeffreycwitt/xgync/src 可见的项目)还创建了到我的/usr/local/bin/xgync
的符号链接,以便我可以从终端的任何位置编写命令xgync {arguments}
.
I'm new to ruby, but I'm working on my first ruby program. It currently has two files, one is a library of functions (xgync.rb
stored in lib
) the other is the executable xgync
stored in 'bin'. (Project visible here https://bitbucket.org/jeffreycwitt/xgync/src) I've also created a symlink to my /usr/local/bin/xgync
so that I can write the command xgync {arguments}
from anywhere in the terminal.
问题似乎是bin/xgync
依赖于库lib/xgync.rb
.我在bin/xgync
中将这种依赖关系编写如下:
The problem seems to be that bin/xgync
depends on the library lib/xgync.rb
. I've written this dependency in bin/xgync
as follows:
$:.unshift(File.dirname(__FILE__) + '/../lib')
require "xgync"
但是,我不断收到以下错误消息:
However, i keep getting the following error:
/Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- xgync (LoadError)
from /Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/bin/xgync:4:in `<main>'
您看到我写的东西有什么问题吗?符号链接会以某种方式弄乱东西吗?
can you see anything wrong with what I've written? Could the symlink be somehow messing things up?
感谢您的帮助:)
推荐答案
使用 ruby 1.9.x 时,在项目中需要其他文件时,通常不会使用$:.unshift
更改路径
When using ruby 1.9.x you don't usually alter the path with the $:.unshift
when requiring other files in your project.
相反,最佳做法是改用require_relative
.
Instead the best practice is to use require_relative
instead.
require_relative '../lib/xgync.rb'
require_relative
需要相对于您当前正在编辑的文件的文件.
require_relative
requires files relative to the file you are currently editing.
但是出现错误,因为您需要一个不存在的文件:
But the error you experience appears, because you require a file, which does not exist:
- bin/xgync
- lib/xgync.rb
- bin/xgync
- lib/xgync.rb
根据您的问题,这些是项目中的文件,并且代码摘录来自 bin/xgync ,您扩展了路径以在 lib/中查找文件但是您尝试使用require 'xgync'
这是一个文件,而该文件在 lib/中不存在,因此,如果要使用此方法(必须使用require 'xgync.rb'
来代替require_relative
.
These are the files in your project according to your question, and the code-excerpt is from bin/xgync you extended the path to look for files in lib/ but you try to require 'xgync'
which is a file, that is not present in lib/, so if you wanted to use this method (instead of require_relative
you would have to use require 'xgync.rb'
.
这篇关于红宝石要求不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!