我正在为一个 Web 应用程序建模,您可以通过单击某个链接进入另一个应用程序页面。我决定做这样的事情:
文件C:/Sandbox/common_page.rb
:
require_relative "./pageA"
require_relative "./pageB"
require_relative "./pageC"
require_relative "./pageD"
class CommonPage
def pageA
# click_pageA_link
pageA.new
end
def pageB
# click_pageB_link
pageB.new
end
def pageC
# click_pageC_link
pageC.new
end
def pageD
# click_pageD_link
pageD.new
end
# and so on for other pages..
end
文件C:/Sandbox/pageA.rb
:
require_relative "./common_page"
class PageA < CommonPage
def pageA
self
end
end
文件C:/Sandbox/pageB.rb
:
require_relative "./common_page"
class PageB < CommonPage
def pageB
self
end
end
类似的代码将呈现给代表其他页面的类。如果我C:/Sandbox/test.rb
使用此代码运行文件:
require_relative "./pageA"
pA = PageA.new
我收到此错误:
C:/Sandbox/pageB.rb:2:in `<top (required)>': uninitialized constant CommonPage (NameError)
from C:/Sandbox/common_page.rb:2:in `require_relative'
from C:/Sandbox/common_page.rb:2:in `<top (required)>'
from C:/Sandbox/pageA.rb:1:in `require_relative'
from C:/Sandbox/pageA.rb:1:in `<top (required)>'
from C:/Sandbox/test.rb:1:in `require_relative'
from C:/Sandbox/test.rb:1:in `<main>'
当我尝试创建其他类的实例时,也会弹出类似的错误。有人可以解释这里发生了什么以及如何避免这个问题吗?