无法将一个类包含到 Ruby 中的另一个类中:未初始化的常量(NameError)
假设我有三个类,每个类都在自己的文件中定义。例如,ClassA.rb 中的 ClassA 等...
class ClassA
def initialize
end
def printClassA
puts "This is class A"
end
end
class ClassB
def initialize
end
def printClassB
puts "This is class B"
end
end
class ClassC
def initialize
end
def bothClasses
a = ClassA.new
b = ClassB.new
a.printClassA
b.printClassB
end
end
如您所见,ClassC 需要其他两个类才能正常运行。我认为,需要有一种方法来导入/包含/加载 ClassC 中的其他两个类。
我是 Ruby 新手,我已经尝试了 load/include/require 的每一种排列,但我不知道如何让它运行。
我通常只是得到:
classc.rb:2:in `<class:ClassC>': uninitialized constant ClassC::ClassA (NameError)
from classc.rb:1:in `<main>'
或者我的 import/include/require 语句出现语法错误。
使用 Windows 7、Ruby 1.9.2、RadRails,所有文件都位于同一项目和源文件夹中。
如果这个问题与这里的其他一些问题类似,我很抱歉,但解决“未初始化常量”的大多数答案是“只需要文件”。我已经尝试过了,但不起作用。
Lets say I have three classs, each define in its own file. e.g. ClassA in ClassA.rb etc...
class ClassA
def initialize
end
def printClassA
puts "This is class A"
end
end
class ClassB
def initialize
end
def printClassB
puts "This is class B"
end
end
class ClassC
def initialize
end
def bothClasses
a = ClassA.new
b = ClassB.new
a.printClassA
b.printClassB
end
end
As you can see, ClassC needs the other two classes in order to function correctly. I assume, there needs to be a way to import/include/load the other two classes in ClassC.
I'm new to Ruby and I've tried every permutation of load/include/require and I cannot figure out how to get this to run.
I normally just get:
classc.rb:2:in `<class:ClassC>': uninitialized constant ClassC::ClassA (NameError)
from classc.rb:1:in `<main>'
Or a syntax error with my import/include/require statements.
Using Windows 7, Ruby 1.9.2, RadRails, all files are in the same project and source folder.
I'm sorry if this question is similar to some of the other questions on here, but most of the answers to resolving the "uninitialized constant" is to "just require the file". I've tried and it isn't working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题是
$:
,控制require
查找文件的变量,不再包含 Ruby 1.9.2 及更高版本中的当前目录(出于安全原因) )。要告诉 Ruby 在哪里查找文件,您需要执行以下操作之一:I think your problem is that
$:
, the variable that controls whererequire
looks for files, no longer includes the current directory in Ruby 1.9.2 and higher (for security reasons). To tell Ruby where to look for the file, you need to do one of:如果我将所有内容都保存在一个文件中,并向代码中添加两行,则它在 1.9.2 上工作正常:
这告诉我代码没问题,问题在于您包含的文件。
我将前两个类分别分成单独的文件“classa.rb”和“classb.rb”,然后将文件修改为:
运行后,我得到了相同的结果,表明它运行正确。
我使用“./path/to/file”,因为它是我正在查找的位置的自我记录,但“path/to/file”,或者在本例中“classa”也可以工作。
然后,我切换到 Ruby 1.8.7,并将
require_relative
行更改为require
,并再次保存文件。从命令行运行它再次正常工作:出于安全原因,Ruby 1.9+ 删除了当前目录“.”从
require
搜索的目录列表中。因为他们知道我们会用干草叉和火把追捕他们,所以他们添加了 require_relative 命令,该命令允许在当前目录及以下目录中进行搜索。If I keep everything in one file, and add two lines to your code it works fine on 1.9.2:
That tells me the code is fine, the problem is in your inclusion of the files.
I split out the first two classes into separate files, "classa.rb" and "classb.rb" respectively, then modified the file to:
After running it I got the same results showing it ran correctly.
I use './path/to/file' because it's self-documenting where I'm looking, but 'path/to/file', or, in this case 'classa' would work as well.
Then, I switched to Ruby 1.8.7, and changed the
require_relative
lines torequire
, and saved out the file again. Running it from the command-line worked correctly again:For security reasons, Ruby 1.9+ removed the current directory '.' from the list of directories searched by
require
. Because they knew we'd hunt them down with pitchforks and torches, they added therequire_relative
command, which allows searching in the current directory and below.