在ruby中编译时出现未定义的方法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ruby中编译时出现未定义的方法错误相关的知识,希望对你有一定的参考价值。

变量origScale在编译时出现错误,即使它是变量,终端也表示未定义的方法。我尝试过更改名称,但是不起作用。

def lessThan(aTemp)
     origScale = @Scale

     aTemp.setF
     setF

     if(@Degrees < aTemp.Degrees ) then
          if(origScale == 'C' or origScale == 'c' ) then
               setC

          elsif(origScale == 'K' or origScale == 'k' ) then
               setK
          end
          return true

     else
          return false
     end
end

我在elsif部分收到错误。它似乎先得到了,但是却给了我在origScale的elsif上未定义的方法错误。

当前错误如下:“ Temperature.rb:177:在'lessThan'中:#的未定义方法'origScale'(NoMethodError)]

答案

您问题中的代码应该有效。任何问题都可能存在于程序的另一部分

NoMethodError仅在接收方上调用方法时出现。您是否可以确保在其他任何地方都未将origScale调用为origScale()或.origScale

的方法?

另外,您可以在origScale上运行.class来查看它是什么类吗?

以上是关于在ruby中编译时出现未定义的方法错误的主要内容,如果未能解决你的问题,请参考以下文章