Ruby 命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16600305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby naming conventions?
提问by Owen_R
For instance, for constants, is it:
例如,对于常量,是不是:
THIS_CONSTANT
THIS_CONSTANT
This_Constant
This_Constant
ThisConstant
ThisConstant
Or something else...?
或者是其他东西...?
In fact, is there any sort of (quasi|)official reference for this whole subject?
事实上,对于整个主题是否有任何(准|)官方参考?
I'd also like to be able to quickly double check questions like:
我还希望能够快速复查以下问题:
What naming patterns are enforced by Ruby itself (eg,
Constants muststart with a capital, right?) and which are simply conventions (method_names should be in snake case, right?)?Are there any conventions for how to write a reminder of what class a variable is supposed to be into its name? ("Hungarian notation" or whatever... I mean, I kinda got the impression that if you ever feel the need to use it in Ruby code, you're Doing It Wrong, but regardless, isthere a convention?)
Ruby 本身强制执行哪些命名模式(例如,
Constants必须以大写开头,对吗?)哪些只是约定(method_names 应该是蛇形大小写,对吗?)?是否有关于如何编写变量应该在其名称中的类的提示的任何约定?(“匈牙利表示法”或任何...我的意思是,我还挺得到的印象是,如果你觉得有必要在Ruby代码中使用它,你就错了,但无论如何,是有一个约定?)
and so on...
等等...
回答by
The Ruby Style Guideis a public effort to recommend consistent conventions and best practices to Ruby developers. You should check it out.
Ruby 风格指南是一项公共努力,旨在向 Ruby 开发人员推荐一致的约定和最佳实践。你应该检查一下。
回答by Purkhalo Alex
In brief
简单来说
The camel case for classes and modules definition we use CamelCase
我们使用的类和模块定义的驼峰案例 CamelCase
The snake case for files, variables and methods: snake_case
文件、变量和方法的蛇形案例: snake_case
回答by John Schmidt
The syntax highlighting in your text editor can give you some good clues as you're learning a language and trying to develop an instinct for its conventions, enforced and otherwise. I use the Linux Gedit editor.
当您正在学习一门语言并试图培养对其约定、强制和其他方面的直觉时,文本编辑器中的语法突出显示可以为您提供一些很好的线索。我使用 Linux Gedit 编辑器。
Yesterday I was pleasantly surprised to find that some of the conventions for commenting recommended by The Ruby Style Guide https://github.com/bbatsov/ruby-style-guide, such as # TODOand # FIXME, show up as bold face bright yellow. Constant names with ALL CAPS are bold cyan. Of course, different editors use different color schemes.
昨天,我惊喜地发现 The Ruby Style Guide https://github.com/bbatsov/ruby-style-guide推荐的一些评论约定,例如# TODOand # FIXME,显示为粗体亮黄色。全部大写的常量名称是粗体青色。当然,不同的编辑器使用不同的配色方案。

