nameerror:_Ruby NameError的原因:未初始化的常量错误

nameerror:

The open-source programming language Ruby is known for its clear syntax and ease of use. That doesn't mean you won't occasionally run into an error message. One of the most vexing is the NameError Uninitialized Constant exception because it has more than one cause. The syntax of the exception follows this format:

开源编程语言Ruby以其清晰的语法和易用性而闻名。 这并不意味着您不会偶尔遇到错误消息。 最令人困扰的问题之一是NameError未初始化的常量异常,因为它有多个原因。 异常的语法遵循以下格式:


NameError: uninitialized c

or

要么


NameError: uninitialized constant

(where various class names are in place of Something)

(其中各种类名代替Something)

Ruby NameError未初始化的常量原因 ( Ruby NameError Uninitialized Constant Causes )

The Uninitialized Constant error is a variation of a regular NameError exception class. It has several possible causes. 

未初始化的常量错误是常规NameError异常类的变体。 它有几种可能的原因。

  • You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class.

    当代码引用无法找到的类或模块时,您会看到此错误,通常是因为代码不包含require ,它指示Ruby文件加载该类。

  • In Ruby, variables/methods begin with lowercase letters, while classes begin with uppercase letters. If the code doesn't reflect this distinction, you'll receive the Uninitialized Constant exception.

    在Ruby中,变量/方法以小写字母开头,而类以大写字母开头。 如果代码没有体现出这种区别,您将收到未初始化的常量异常。
  • Still another possible cause for the NameError error is that you've made a simple typo in the code. 

    NameError错误的另一个可能原因是您在代码中做了一个简单的错字。
  • Ruby is case sensitive, so "TestCode" and "Testcode" are completely different. 

    Ruby区分大小写,因此“ TestCode”和“ Testcode”完全不同。
  • The code contains mention of rubygems, which is deprecated in all but old versions of Ruby.

    该代码包含rubygems ,在旧版本的Ruby中都已弃用该代码。

如何解决错误 ( How to Fix the Error )

To troubleshoot your code, examine it for the possible causes listed above one at a time. If you find a problem, address it. For example, go through the code looking for a discrepancy in uppercase and lowercase usage on variables and classes. If you find one and correct it, your problem is probably solved. If it isn't, continue through the other possible causes, fixing as you go.

要对代码进行故障排除,请一次检查一下上面列出的可能原因。 如果发现问题,请解决。 例如,遍历代码以查找变量和类的大写和小写用法之间的差异。 如果找到并纠正它,则可能会解决您的问题。 如果不是这样,请继续寻找其他可能的原因,并根据需要进行修复。

If the class you refer to in the code is in another module, refer to it with its full name like this:

如果您在代码中引用的类在另一个模块中,请使用其全名来引用它,如下所示:


#!/usr/bin/env rubymodule MyModule class MyClass; endendc = MyMo

关于Ruby异常 ( About Ruby Exceptions )

Exceptions are how Ruby draws your attention to problems in the code. When an error in the code is encountered, an exception is "raised" or "thrown" and the program shuts down by default.

异常是Ruby吸引您注意代码中问题的方式。 当代码中遇到错误时,将“引发”或“引发”异常,并且默认情况下该程序关闭。

Ruby publishes an exception hierarchy with predefined classes. NameErrors are in the StandardError class, along with RuntimeError, ThreadError, RangeError, ArgumentError and others. This class includes most of the normal exceptions that you encounter in typical Ruby programs.

Ruby发布带有预定义类的异常层次结构。 NameErrors与RuntimeError,ThreadError,RangeError,ArgumentError等一起在StandardError类中。 此类包含典型Ruby程序中遇到的大多数常规异常。

翻译自: https://www.thoughtco.com/nameerror-uninitialized-2907928

nameerror:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值