在 Ruby 中包含模块
我正在尝试包含一个 Ruby 模块。
在文件 helper.rb 中,我有这样的文本
module Helper
...
end
在文件 test.rb 中,我有这样的文本:
....
require 'helper'
...
这些文件位于目录的同一级别,但我不断收到此错误:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:4:in `<main>'
我也尝试
include Helper
过 test.rb 并得到这个错误:
test.rb:4:in `<main>': uninitialized constant Object::Helper (NameError)
我做错了什么?
I am trying to include a Ruby module.
In the file helper.rb, I have this text
module Helper
...
end
In the file test.rb, I have this text:
....
require 'helper'
...
These files are on the same level of the directory yet I keep getting this error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:4:in `<main>'
I have also tried
include Helper
in test.rb and get this error:
test.rb:4:in `<main>': uninitialized constant Object::Helper (NameError)
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Ruby 1.9 中你应该使用
In Ruby 1.9 you should use
尝试
require './helper'
。应该可以做到这一点。Try
require './helper'
. That should do it.