[英]Ruby 1.8.7: Can't load module from custom gem
After creating a custom gem I'd like to reference functions defined in a module it contains. 创建自定义gem之后,我想引用其包含的模块中定义的函数。
Custom gem is dsi_core
and the module lib/cuke.rb
contains simply: 定制gem是
dsi_core
,模块lib/cuke.rb
包含以下内容:
module DsiCore
module Cuke
def self.Features(*args)
puts "Hello world!"
end
end
end
I've installed the gem and can see /var/lib/gems/1.8/gems/dsi_core-0.1.0/lib/cuke.rb
exists as it should. 我已经安装了gem,可以看到
/var/lib/gems/1.8/gems/dsi_core-0.1.0/lib/cuke.rb
确实存在。
In another gem dsi_fabric
there is the code dsi_fabric/lib/dsi_fabric
: 在另一个gem
dsi_fabric
有代码dsi_fabric/lib/dsi_fabric
:
require 'dsi_core'
# ...
DsiCore::Cuke.Features(*ARGV)
Trying to run this from the lib/
directory of dsi_fabric
gem: 尝试从
dsi_fabric
gem的lib/
目录运行此dsi_fabric
:
ruby -rubygems ./dsi_fabric.rb arg1 arg2
..results in: ..结果是:
./dsi_fabric.rb:7: uninitialized constant DsiCore::Cuke (NameError)
This refers to the Cuke.Features
line in dsi_fabric.rb
. 这是指
Cuke.Features
符合dsi_fabric.rb
。
What am I doing wrong? 我究竟做错了什么?
You're requiring 'dsi_core' instead of 'cuke'. 您需要“ dsi_core”而不是“ cuke”。 Try changing
require 'dsi_core'
to require 'cuke'
. 尝试将
require 'dsi_core'
更改为require 'dsi_core'
require 'cuke'
。
If you are testing in existing namespace problem can be namespace, so try with root namespace reference. 如果在现有名称空间中进行测试,则问题可能是名称空间,因此请尝试使用根名称空间引用。
::DsiCore::Cuke.Features(*ARGS)
also like I mentioned in comment you need to put 也就像我在评论中提到的那样,您需要输入
def self.Features(*args)
# ...
end
Hehe I do not like that capital method name annotation but to each his own :) 呵呵,我不喜欢那种大写方法名称注解,但是喜欢每个人自己的:)
EDIT 编辑
Also try 也试试
require 'rubygems'
require 'dsi_core'
I was miscomprehending how Gem imports work. 我误会了宝石进口的工作方式。
If you create a gem and add other gem dependencies to it in the gemspec, those dependencies are automagically imported already. 如果创建了gem并在gemspec中添加了其他gem依赖项,则这些依赖项已被自动导入。
Therefore what I have above is wrong - to import the cuke.rb
file from gem dsi_core
in gem dsi_fabric
you would use: 因此,我上面所说的是错误的-从gem
dsi_core
中的gem dsi_fabric
导入cuke.rb
文件,您将使用:
require 'cuke'
ie the require dsi_core
is implicit by nature of the Gem specification and gemspec. 即
require dsi_core
是Gem规范和gemspec的本质所隐含的。
Then to reference in the code would be 然后在代码中引用将是
DsiCore::Cuke
..as I originally posted. ..就像我最初发布的那样。
I was confused because mny public gems indicate you should use require
with the form: 我很困惑,因为mny public gems指示您应将
require
与以下形式配合使用:
require `gem-name/module-or-class`.
However this is misleading: the gem name isn't being used at all, but rather a directory named after the gem: 但这有误导作用:根本没有使用gem名称,而是使用了以gem命名的目录:
require 'dir-name/module-or-class'
In this example the gem gem-name
has the following directory structure: 在此示例中,gem
gem-name
具有以下目录结构:
lib/gem-name/module-or-class.rb
ie they've made a folder in the gem's lib/
directory named after the gem itself. 也就是说,他们在gem的
lib/
目录中创建了一个以gem本身命名的文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.