本文介绍了Ruby中的NameError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这段代码:

class myBaseClass
  def funcTest()
    puts "baseClass"
  end
end
myBaseClass.new.funcTest

I得到一个错误:

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 ,我得到:

When I tryx=myBaseClass.new, I get:

NameError: undefined local variable or method `myBaseClass' for main:Object from (irb):152

有人已经遇到这个问题吗?我不认为我的代码可能是错误的。

Has someone already encountered this problem? I don't think my code can be wrong.

推荐答案

在ruby中,所有常量包括类名称必须以大写字母开头。 myBaseClass 将被解释为未定义的局部变量。 MyBaseClass 将正常工作。

In ruby, all constants including class names must begin with a capital letter. myBaseClass would be interpreted as an undefined local variable. MyBaseClass would work properly.

这篇关于Ruby中的NameError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 18:34