无法将一个类包含到 Ruby 中的另一个类中:未初始化的常量(NameError)

发布于 2024-10-16 05:12:14 字数 896 浏览 4 评论 0原文

假设我有三个类,每个类都在自己的文件中定义。例如,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷炫老祖宗 2024-10-23 05:12:14

我认为你的问题是 $: ,控制 require 查找文件的变量,不再包含 Ruby 1.9.2 及更高版本中的当前目录(出于安全原因) )。要告诉 Ruby 在哪里查找文件,您需要执行以下操作之一:

require_relative 'ClassA' # which looks in the same directory as the file where the method is called

# or #

require './ClassA' # which looks in the current working directory

I think your problem is that $:, the variable that controls where require 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:

require_relative 'ClassA' # which looks in the same directory as the file where the method is called

# or #

require './ClassA' # which looks in the current working directory
堇色安年 2024-10-23 05:12:14

如果我将所有内容都保存在一个文件中,并向代码中添加两行,则它在 1.9.2 上工作正常:

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

c = ClassC.new
c.bothClasses
# >> This is class A
# >> This is class B

这告诉我代码没问题,问题在于您包含的文件。

我将前两个类分别分成单独的文件“classa.rb”和“classb.rb”,然后将文件修改为:

require_relative './classa'
require_relative './classb'

class ClassC

  def initialize
  end

  def bothClasses
    a = ClassA.new
    b = ClassB.new
    a.printClassA
    b.printClassB
  end
end

c = ClassC.new
c.bothClasses

运行后,我得到了相同的结果,表明它运行正确。

我使用“./path/to/file”,因为它是我正在查找的位置的自我记录,但“path/to/file”,或者在本例中“classa”也可以工作。

然后,我切换到 Ruby 1.8.7,并将 require_relative 行更改为 require,并再次保存文件。从命令行运行它再次正常工作:

require './classa'
require './classb'

class ClassC

  def initialize
  end

  def bothClasses
    a = ClassA.new
    b = ClassB.new
    a.printClassA
    b.printClassB
  end
end

c = ClassC.new
c.bothClasses

出于安全原因,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:

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

c = ClassC.new
c.bothClasses
# >> This is class A
# >> This is class B

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:

require_relative './classa'
require_relative './classb'

class ClassC

  def initialize
  end

  def bothClasses
    a = ClassA.new
    b = ClassB.new
    a.printClassA
    b.printClassB
  end
end

c = ClassC.new
c.bothClasses

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 to require, and saved out the file again. Running it from the command-line worked correctly again:

require './classa'
require './classb'

class ClassC

  def initialize
  end

  def bothClasses
    a = ClassA.new
    b = ClassB.new
    a.printClassA
    b.printClassB
  end
end

c = ClassC.new
c.bothClasses

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 the require_relative command, which allows searching in the current directory and below.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文