Ruby 中的标准文件命名约定
对于包含给定类 SomeCoolClass 的文件,正确的或标准的文件名是什么?
1. somecoolclass.rb 2. some_cool_class.rb 3. some-cool-class.rb 4. SomeCoolClass.rb
或者其他一些变化?
我注意到在 Ruby stdlib 中使用了版本 1、2 和 3。
For a file containing the given class, SomeCoolClass, what would be the proper or standard filename?
1. somecoolclass.rb 2. some_cool_class.rb 3. some-cool-class.rb 4. SomeCoolClass.rb
or some other variation?
I noticed in the Ruby stdlib, versions 1, 2 and 3 are used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于Ruby(即不是Rails),命名只是一种约定。 在 Rails 中,使用下划线的约定(几乎)是必要的。
我认为约定 #2
lowercase_and_underscore.rb
更常见,看起来也不错,尽管一篇文章 这里表示lowercasenounderscore.rb
是Ruby约定。选择哪种约定更常见或者您更喜欢哪种约定。 最重要的是在项目内保持一致。
With just Ruby (i.e. not Rails), naming is only a convention. In Rails the convention of using underscores is necessary (almost).
I think convention #2
lowercase_and_underscore.rb
is more common and looks pretty good, though an article Here sayslowercasenounderscore.rb
is the Ruby convention.Pick either which ever convention is more common or which ever one you like more. The most important thing is to be consistent within a project.
我个人认为连字符作为单词分隔符通常可以实现最大的可读性和可输入性,因此我建议尽可能使用连字符(在某些情况下,不能使用连字符,例如在大多数语言的标识符中)。 需要记住的一件重要事情是,您选择的方案将影响用户将在您的库中使用的 require 语句,并且您希望避免使用与库名称不同的 gem 名称。
Bad
Good
不幸的是,少数库违反了这个简单的可用性规则。 不要成为这些图书馆之一。 :)
I personally think the hyphen as word separator makes for maximum readability and typability in general, so I recommend that where possible (in some contexts, a hyphen can't be used, such as in identifiers in most languages). One important thing to bear in mind is that the scheme you pick will have a bearing on the require statement that users will use with your lib, and you want to avoid having a different gem name than library name.
Bad
Good
Unfortunately, a small handful of libraries violate this simple usability rule. Don't be one of those libraries. :)
我建议使用带下划线的小写字符(您问题中的数字 2)。 确实,这种命名方案是 Rails 中的约定,在非 Rails 项目中没有必要。 然而,我仍然会坚持 Rails 约定,因为大多数 Ruby 程序员可能只将 Ruby 用于 Rails。
I would recommend lower case characters with underscores (number 2 in your question). It's true that this naming scheme is the convention in Rails and not necessary in non-Rails projects. However, I would still stick to the Rails convention because most Ruby programmers are probably using Ruby exclusively for Rails anyway.