- 浏览: 329159 次
- 性别:
- 来自: 北京
文章分类
最新评论
Ruby编码规范
File Names
目录、文件名、扩展名
Ruby 源代码
目录名和文件名要用小写字母命名,类和模块以.rb为结尾
例如:
Foo class => foo.rb Bar module => bar.rb FooBar class => foobar.rb (Normal Rule) foo_bar.rb (Rails Rule) Foo::Bar class => foo/bar.rb
:Libraries((any arguments?))
non-standard multiple files required by a script should be in one directory, and they should be installed to the standard site-ruby directory or a script’s library directory.
don’t use a current directory for libraries-it is dangerous. since the directory in which script exists can not be specified portably, it is also not suited for libraries.
(你的程序加载的非标准类库文件应该放在一个文件夹内,并且安装到site-ruby文件夹或者是程序的lib文件夹。别用当前文件夹做库文件-这是危险的。既然你的程序所在的目录不能指定,那么lib目录里面也同样。)
File Organization(文件组织)
require (if necessary)
include (if necessary)
classes and modules definition
main
testing code(?) usingFILEidiom
ex:
if FILE == $0 end
Beginning Comment(注释)
begin – end style(begin-end型注释)
for file header or footer.(放在文件头或文件结尾)
=begin * Name: * Description * Author: * Date: * License: =end
using ’#’ style(“#”类型注释)
for class/module and method definitions(用在类或模块或方法定义中)
# using foo bar # # foo bar buz. # class Foo # bar.bar.bar. # class Bar # constructor of Bar. # def initialize(bar = nil) super(bar) end def bar self end end def initialize() end end
Indentation(代码缩进)
Line length(每行代码长度)
Max length is about 80 characters.(最大宽度为80字符)
Wrapping Line(wrapping 行)
Break after comma(在逗号处换行)
Break after operator(在操作符处换行)
Comments(注释)
Ruby has two comments style: =begin…=end and ’#’. You should use =begin…=end for Documentation Comments, and ’#’ for both Documentation Comments and Implementation Comments.(ruby有两种注释方式:=begin…=end and ’#’。=begin…=end用于文档注释,而‘#’既可以用于文档注释又可以用于类和模块或者方法的实现注释)
Implementation Comments
h4. Block Comments(块注释)
# Here is block comment. # # foo, bar, buz.
h4. Single-Line Comments(单行注释)
# this is single line comment. ## this is also single line comment.
h4. Tailing Comments(行尾注释)
if a == 2 true # special case else prime?(a) # work only for odd a end
h3. Documentation Comments h4. Header/Footer(文档注释,放在头部或尾部)
=begin = FooBar library == What's New? ..... .... == Installation ..... .... .... =end
h4. In Class/Module(在类或模块中)
# ..... # .... # def foo() .. or ## # ..... # .... # def foo() ..
h3. Way of no commenting(如何不需要注释)
If you can write simple, short and light scripts, comments may not be necessary.(如果你的代码简短易懂你可以不必注释)
You can let ((the script itself tell everything)), instead of embedding documentation that may confuse readers of your script.(如果你能让你的程序自己说明一切,而不是嵌入让使用你的程序的人抓狂的文档那就更好)
Definitions(声明)
Initialization(初始化)
Ruby variables have no ‘definitions’. So, you should initialize variables.(Ruby变量不需要声明,所以你应该初始化变量)
One initialization per line(每行初始化一个变量)
level = 0 size = 0
is preferred over(这样是更好的)
level = size = 0
Placement(布局)
Statements(表达式)
Simple Statements(简单的表达式)
Each line should contain at most one statement.(每行应该至少包含一个表达式)
foo = 1 ## Correct bar = 2 ## Correct foo = 1; bar = 2 ## AVOID!
Compound Statements(复合表达式)
if-end, if-else-end, if-elsif-else-end Statements(if-end, if-else-end, if-elsif-else-end表达式)
simple example:(简单举例:)
if end
more complex example:(更复杂一点的例子)
if elsif else end
You can put on after when is one-line.(如果表达式只有一行,你可以把if放在后面(puts “Hooopo” if true))
if
block methods(模块方法)
`{...}’ style:(‘{}’类型)
bar.foo(vars){|vars2| }
`do…end’ style:(‘do...end’类型)
bar.foo(vars) do |vars2|
end
one-line block:(一行的代码块:)
bar.foo(){|var| }
case-when Statements(case-when表达式)
Ruby’s case-when (not when-case) does not need ‘break’.(ruby的case-when(不是when-case)不需要break)
case foo when condition1 when condition2 else end
begin-rescue-end Statements(regin-rescue-end表达式)
It handles errors (Exceptions).(它是用来处理异常的)
begin rescue FooError => e rescue BazError => e2 rescue end
White Space(留白)
Blank Lines(空行)
Between sections of a source file(在几部分源代码之间)
Between class and module definitions(在类和模块定义之间)
Between methods(在方法之间)
Before blocks or single-line comments(在代码块和单行注释之前)
Between logical sections inside a method to improve readability(在方法中用空行来表示算法逻辑以提高可读性)
Blank spaces(空格)
A keyword followed by a parenthesis should be separated by a space.(关键字和后面的括号之间应该用空格分开)
ex:
while (foo.end?) { }
The number of spaces should be balanced.(空格的数量应该保持一致)
a+b ## Correct a + b ## Correct a+ b ## AVOID! a +b ## AVOID! (Erroneous: interpreted as a(+b)) a += b + c a = (a + b) / (c * d) a = b foo("foo" + buz + bar)
Naming Conventions(命名规范)
Classes/Modules(类和模块)
class and module names should be nouns; in mixed case with the first letter of each internal word capitalized.(类名和模块名用名词;如果是多个词,用每个词首字母大写后链接)
ex:
class Raster, class Raster::ImageSprite
Methods(方法)
Methods should be verbs. All lower caseASCIIletters with words separated by underscores (’_’)(方法名用动词,用小写ASCII字符以下划线相连)
ex.
run(), run_fast(), obj.background_color()
Variables(变量)
variable names should be all lower caseASCIIletters with words separated by underscore (’_’)(变量名用小写字母和下划线)
ex:
i = 1 some_char = SomeChar.new() table_width = 0.0
Constants(常量)
constants should be all upper case with words separated by underscores (’_’). ((Huh, is there a reasonable background to distinguish constants from a class name which is a constant at the same time?))(常量是由大写字母和下划线组成)
ex:
MIN_LENGTH = 1 DEFAULT_HOST = "foo.example.com"
Omission(补充)
Speaking of ‘Connection Pool’ as a variable, you should decide to prefer name by scope such as the following…(说到想把‘Connection Pool’当做变量,变量形式是由下面哪种上下文决定的)
‘conpool’ for local scope (such as local variable)(‘conpool’在全局范围做全局变量)
’@connection_pool’ for class scope (such as instance variable)(‘@connection_pool’在类内部用做实例变量)
Pragmatic Programming Practices(编码实践)
Using attr_* to access
def foo() @foo end attr_reader :foo
Without Parenthesis(省略括号)
Some methods are used without parenthesis.(一些方法可以省略括号)
require
ex.
require 'foo/bar'
include
ex.
include FooModule p
ex.
p foo attr_*
ex.
attr_reader :foo, :bar
Reduce repetition(减少重复)
When successive lines of a script share something,(当连续的代码有公用的内容时)
x = ModuleA::ClassB::method_c( a ) y = ModuleA::ClassB::method_d( b )
(- ‘function’ => ‘method’ -)
you should make it like this:(你可以这样)
cb = ModuleA::ClassB x = cb::method_c( a ) y = cb::method_d( b )
You can also do:(也可以这样)
include ModuleA x = ClassB::method_c(a) y = ClassB::method_d(b)
Code Example(代码示例)
h3. Ruby Source File Example
=begin blahdy/blah.rb $Id:$ Copyright (c) 2001 TAKAHASHI Masayoshi This is free software; you can copy and distribute and modify this program under the term of Ruby's License (http://www.ruby-lang.org/LINCENSE.txt) =end # # module description goes here. # # @version: 1.82 # @author: TAKAHASHI Masayoshi # module Blahdy class Blah < SomeClass # A class implementation comment goes here. # CLASS_VAR1 documentation comment CLASS_VAR1 = 1; # CLASS_VAR2 documentation comment that happens # to be more than one line length. # CLASS_VAR2 = 1; # ...constructor Blah documentation comment... # def initialize() ## ...implementation goes here... end # ...method do_something documentation comment... # def do_sometiong() ## ...implementation goes here... end # ...method do_something_else documentation comment... # # @param some_param description # def do_something_else(some_param) ## ...implementation goes here... end end end
原文地址:http://ruby-programming.learnhub.com/lesson/5017-ruby-coding-convention
在线文档:http://docs.google.com/View?docid=ddf5zdwx_3czk7jhf7
评论
4 楼
qxt
2009-09-09
hooopo翻译的不错
3 楼
shuchaoo
2009-09-07
哈哈!翻译得确实!不过有原文地址就行了!
2 楼
Hooopo
2009-04-19
blackwolf1983 写道
老兄,你连贯着翻译行不,看着累死人了。
I See,以后注意。
1 楼
blackwolf1983
2009-04-19
老兄,你连贯着翻译行不,看着累死人了。
发表评论
-
新博客
2012-04-23 20:47 1592https://db-china.org -
Ruby Verbose Warning Mode
2011-10-16 14:48 2018Ruby在很多方面是一个更优雅的Perl,从Perl社区继承了 ... -
Pattern Match In Ruby
2011-10-07 01:17 1965最近看了一些Erlang,模式匹配是个好东西,简单的sum函数 ... -
Draper: View Models for Rails
2011-10-07 01:19 2228Draper是一个Ruby gem,它让Rails model ... -
Active Record batch processing in parallel processes
2011-10-07 01:20 2227Active Record 提供 find_each来分批处理 ... -
最轻量级的Ruby后台任务
2011-08-04 16:47 3815普通情况下ruby调用系统命令行的过程是堵塞的,无论是用sys ... -
test
2011-07-15 19:59 0test -
fiber
2011-06-17 09:37 0挖坑,待填。。 1.用到fiber.alive?、fiber ... -
Identity Map in Rails3.1
2011-06-12 18:29 2703Identity Map是Rails3.1的又 ... -
xx00
2011-06-06 03:40 0https://github.com/ngmoco/cache ... -
挖坑1
2011-06-06 02:17 0cache money 源码 替换memcache为redis ... -
websocket demo
2011-06-04 20:44 2020地址:https://github.com/hooopo/we ... -
ruby GC
2011-06-02 04:24 0http://blog.csdn.net/lijun84/a ... -
reduce method missing call stack with dynamic define method
2011-04-22 22:54 1551method_missing是ruby里面一个非常cool的h ... -
Autocompete with Trie
2011-04-09 04:04 1627像微薄里面用户输入一 ... -
用imagemagick和tesseract-ocr破解简单验证码
2011-04-09 01:31 18854工具:imagemagick + tesseract-ocr ... -
OAuth gem for rails,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
2011-03-26 03:13 4442地址:https://github.com/hooopo/oa ... -
用jmeter模拟amf请求进行压力测试
2010-12-16 16:56 29761.获取amf二进制包: 在本地建立proxy,端口为888 ... -
Memoization in Ruby
2010-11-14 11:42 1160这里的Memoization就是将ruby的方法或lambda ... -
整理了一下2008-2010的RubyHeroes博客列表
2010-10-07 02:26 2793Bryan Helmkamp(webrat作者)https:/ ...
相关推荐
10.5 多态、为什么ruby、ruby编码规范.rar
类变量、全局变量、实例变量, 多态、为什么ruby、ruby编码规范
编码规范:在理想的世界里,遵循这些准则 源代码布局 所有源文件以UTF-8编码 使用2个空格的缩进 使用Unix风格的换行符(\n) 每行不超过80字符 每行的结尾不要有空白字符 语法规范 方法如果没参数就省略括号,有参数就...
Ruby 注释 注释会对 Ruby 解释器隐藏一行,或者一行的一部分,或者若干行。您可以在行首使用字符( # ): # 我是注释,请忽略我。 或者,注释可以跟着语句...Ruby 中文编码 用 Ruby 输出 “Hello, World!”,英文没
RUBY版本:ruby 2.1.1p76(这受cloud9 IDE包含在其服务器上的限制) 我们将创建各种编码挑战和答案。 从命令行运行“ rspec”将执行测试。 通读故障,将使您对编码内容有所了解。 第一个规范文件是关于字符串...
3.1 Java源文件(Java Source Files)每个Java源文件都包含一个单一的公共类或接口 3.1.3 类和接口声明(Class and Int
.下面是一些常见错误的介绍:Checkstyle的结果输出序号 输出内容意义1 Type is missing a javadoc commentClass 缺
BERT 是 Ruby 的 BERT (Binary ERlang Term) 序列化库,可以编码 Ruby 对象到 BERT 格式,或者解码 BERT 二进制到 Ruby 对象。BERT 规范请看 bert-rpc.org。以下的 Ruby 类实例会被自动转化为简单的 BERT 类型:...
智威汤逊 标准的Ruby实现。... 强烈建议您对算法进行硬编码,因为通过动态选择算法可能会使自己容易受到攻击 请参阅: 没有 无-未签名的令牌 require 'jwt' payload = { data : 'test' } # IMPORTANT: s
顺从的[ ] 与其让每个Ruby项目重塑这个轮子,不如让我们的Ruby DSL编码更加温顺...用法基本:Ruby 作为DSL 假设我们要创建一个用于修改Array对象的DSL。 如果仅将Array的方法视为DSL会不会很好? with_array ( [ ] ...
这是第一套也是唯一一套在 Ruby 语言中使用古典希腊语的工具。 它们旨在在其范围内直接、轻量级和全面。 也就是说,这是一项正在进行的工作,这个工具集将随着我对它们的需求而继续增长。 我绝对在这里挠痒痒。 ...
Ruby的JSON实现 描述 ... 所有要编码为JSON字符串的字符串,在Ruby端均应为UTF-8字节序列。 要对未经UTF-8编码的原始二进制字符串进行编码,请使用String的to_json_raw_object方法(该方法生成一个
以下规范特定于二维方形网格上的 turmite,这是研究最多的 turmite 类型。 其他网格上的 Turmites 可以用类似的方式指定。 与 Langton's ant 一样,turmites 在每个时间步执行以下操作: 原地转动(90°的倍数) ...
拉克迪斯 很棒的服务器的 Ruby 克隆,允许... 然而,这段代码大多未经测试,规范即将推出,所以东西可能还不能正常工作(因此-beta )。 我在 Argument Parser 和 Response Builder 区域仍有工作要做,因此与经典COMM
快速二进制编码(FBE)快速二进制编码允许描述任何域模型,...快速二进制编码文档快速二进制编码下载快速二进制编码规范可在此处找到与其他协议的性能比较:协议消息大小序列化时间反序列化时间Cap'n'Proto 208字节55
Stupidedi使您可以直接在Ruby中编码这些事务集规范。 根据这些规范,它将生成解析器以读取传入消息,并生成DSL以生成传出消息。 与从头开始编写解析器相比,此方法具有巨大的优势,因为解析器容易出错并且难以更改...
回想一下你的编码方式。 您创建网页的一部分,然后浏览到该页面以对其进行测试,以确保它按照您的预期运行。 然后添加另一个功能。 并测试这两个功能。 然后你添加第三个功能并测试...只是第三个功能。 想象一下,...
规范编码为26个字符串,而不是36个字符的UUID 使用Crockford的base32获得更好的效率和可读性(每个字符5位) 不区分大小写 没有特殊字符(URL安全) 安装 gem install ulid 用法 require 'ulid' ULID . generate ...
Beansmile代码规范指南说明:本文档用于Beansmile内部代码规范,包括HTML,CSS,JavasSript,Ruby,Rails编码规范。本文档的目的是:提高代码可读性保持代码一致性发布于 源码:规范的收录原则:应该有实际指导意义...
要运行Ruby规范,请在终端中执行rspec命令 JavaScript说明: 要运行JavaScript规范,请在终端中执行rake jasmine命令。 然后打开Web浏览器访问 算法与数据结构 Key - (R) means Ruby - (JS) means JavaScript - (P...