对于这段代码:
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 `<main>'
irb(main):152:0> x=myBaseClass.new
当我尝试时x=myBaseClass.new,我得到:
NameError: undefined local variable or method `myBaseClass' for main:Object from (irb):152
有人已经遇到过这个问题吗?我不认为我的代码可能是错的.
edg*_*ner 50
在ruby中,包括类名在内的所有常量必须以大写字母开头.myBaseClass将被解释为未定义的局部变量.MyBaseClass会正常工作.
你的类名应该以大写开头,下面的工作代码
class MyBaseClass
  def funcTest()
   puts "baseClass"
 end
end
MyBaseClass.new.funcTest