Rails 禁止名称模型的命名约定
我正在编写一个 Rails 应用程序,我需要将我的模型命名为 Test,在该模型中我需要一个类属性 - 当我尝试第一个模型时,我无法运行任何 UT,因为 Test 不再是一个模块- 所以我改名为Tst。我什至没有尝试命名列类 - 我选择了 clss。你会用什么名字? RoR 开发人员是否有针对此类情况的约定?
I'm writing a rails application, and I need to name one of my model Test, and inside that model I need a class attribute - when I tried the first one, I couldn't run any UT since Test ceased to be a module - so I ranamed to Tst. I haven't even tried naming a column class - I went with clss. What names would you use? Are there any conventions known amongs RoR developers for such situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我还没有看到任何测试的内容,但类通常会更改为 klass。
I haven't seen anything for Test but class is typically changed to klass.
Ruby 和 Rails 在标准库中有几个不同的对象部分。您不能使用已定义的类名称作为模型的名称。
例如,您无法调用模型
Thread
或Object
,因为 Ruby 已经定义了Thread
和Object
类。同样,Ruby 有一个名为
Test::Unit
的库,因此您不能使用Test
命名空间作为模型名称。没有真正的保留对象的完整列表,因为它实际上取决于您当前的环境。但是,为了成功使用 Rails,您至少应该具备基本的 Ruby 知识,以便知道最常见的标准库类的名称。
Ruby and Rails have several different objects part of a standard library. You cannot use an already-defined class name as name for your models.
For instance, you cannot call a model
Thread
orObject
because Ruby already defines aThread
andObject
class.Likewise, Ruby has a library called
Test::Unit
so you can't use theTest
namespace as model name.There isn't a real full list of reserve objects because it really depends on your current environment. However, in order to successfully use Rails, you should at least have a basic Ruby knowledge so that you know the names of the most common standard library classes.
我已经遇到过几次这样的问题(编写教育软件——你经常想要对“测试”进行建模:-)。我想这完全取决于你要建模的内容,但我通常选择“测验”以避免冲突。我很想听到更好的解决方案,因为我发现“测验”是一个尴尬的复数。
就像 dj2 所说,班级通常被称为“klass”。
I've run up against this a few times (writing educational software--where you regularly want to model 'tests' :-). Depends exactly what you're modeling I suppose, but I usually opt for 'quiz' to avoid conflicts. I'd love to hear a better solution, since I find 'quizzes' an awkward plural.
Like dj2 said, class is usually done as 'klass'.
请检查以下页面以了解 Rails 保留字: http://reservedwords.herokuapp.com/ 这些字都不是应该用作类或属性名称。
Please check the following page for Rails reserved words: http://reservedwords.herokuapp.com/ None of these words should be used as class or attribute name.