Ruby中的NameError
对于这段代码:
class myBaseClass def funcTest() puts "baseClass" end end myBaseClass.new.funcTest
我收到一个错误:
NameError: undefined local variable or method `myBaseClass' for main:Object from c:/Users/Yurt/Documents/ruby/polymorphismTest.rb:9 from (irb):145:in `eval' from (irb):145 from c:/Ruby192/bin/irb:12:in `' irb(main):152:0> x=myBaseClass.new
当我尝试x=myBaseClass.new
,我得到:
NameError: undefined local variable or method `myBaseClass' for main:Object from (irb):152
有人已经遇到过这个问题吗? 我不认为我的代码可能是错的。
在ruby中,包括类名在内的所有常量必须以大写字母开头。 myBaseClass
将被解释为未定义的局部变量。 MyBaseClass
可以正常工作。
您的class级名称应以下面的大写,工作代码开头
class MyBaseClass def funcTest() puts "baseClass" end end MyBaseClass.new.funcTest
你的代码错了。 类名必须以Ruby中的大写字母开头。
class MyBaseClass
解决它。
我没有得到的是你怎么没有像我一样得到明确的错误信息。