`
heamon
  • 浏览: 20969 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

http://pub.cozmixng.org

    博客分类:
  • Ruby
阅读更多
= RubyCodingConvention

(DRAFT VERSION: 2001/11/01)

== File Names

=== Directory and File names and Suffixes

:Ruby source code

file/directory name is lower case class/module name with
suffix '.rb'.

ex.
    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.

== File Organization

* require (if necessary)
* include (if necessary)
* classes and modules definition
* main
* testing code(?) using __FILE__ idiom

    ex:
      if __FILE__ == $0
        <testcode>
      end

=== Beginning Comment

==== begin - end style

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.

=== Wrapping Line

* 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.

=== Implementation Comments

==== Block Comments

  # Here is block comment.
  #
  #  foo, bar, buz.

==== Single-Line Comments

  # this is single line comment.

  ## this is also single line comment.

==== Tailing Comments

  if a == 2
    true        # special case
  else
    prime?(a)   # work only for odd a
  end

=== Documentation Comments

==== Header/Footer

  =begin
 
  = FooBar library
 
  == What's New?
  .....
  ....
 
  == Installation
  .....
  ....
 
  ....
   
  =end

==== In Class/Module

  # .....
  # ....
  #
  def foo()
    ..

or

  ##
  # .....
  # ....
  #
  def foo()
    ..

=== 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.

=== 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

  simple example:

    if <condition>
      <statements>
    end

  more complex example:

   if <condition>
     <statements>
   elsif <condition>
     <statements>
   else
     <statements>
   end

You can put on <condition> after <statements>
when <statements> is one-line.

  <statements> if <condition>

=== block methods

`{...}' style:

    bar.foo(vars){|vars2|
      <statements>
    }

`do...end' style:

    bar.foo(vars) do |vars2|
      <statements>
    end

one-line block:

    bar.foo(){|var|  <statements> }

=== case-when Statements

Ruby's case-when (not when-case) does not need 'break'.

  case foo
  when condition1
    <statements>
  when condition2
    <statements>
  else
    <statements>
  end

=== begin-rescue-end Statements

It handles errors (Exceptions).

  begin
    <statements>
  rescue FooError => e
    <statements>
  rescue BazError => e2
    <statements>
  rescue
    <statements>
  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?) {
      <statements>
    }

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 case ASCII letters with
words separated by underscores ('_')

  ex.  run(), run_fast(), obj.background_color()

=== Variables

variable names should be all lower case ASCII letters 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...

* 'conpool' for local scope (such as local variable)
* '@connection_pool' for class scope (such as instance variable)

== 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

=== 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
分享到:
评论

相关推荐

    中文版RFC文档

    来自以下&lt;br/&gt;组织:中国互动出版网(http://www.china-pub.com/)&lt;br/&gt;RFC文档中文翻译计划(http://www.china-pub.com/compters/emook/aboutemook.htm)&lt;br/&gt;E-mail:ouyang@china-pub.com&lt;br/&gt;译者:charliechen...

    Foxit PhantomPDF Business 5注册

    1、下载安装Foxit PhantomPDF Business 5(福昕PDF电子文档处理套件企业版),...http://cdn01.foxitsoftware.com/pub/foxit/phantomPDF/desktop/win/5.x/5.0/chs/FoxitPhantomPDF504_Business_chs_Setup.msi 64位: ...

    nginx安装教程

    sudo ./configure sudo make sudo make install # 安装zlib库 cd /usr/local/src sudo wget http://zlib.net/zlib-1.2.11.tar.gz sudo tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 sudo ./configure sudo make ...

    https://dl-ssl.google.com/linux/linux-signing-key.pub

    https://dl-ssl.google.com/linux/linux_signing_key.pub

    MQTT mosquitto 服务端客户端Demo 可直接使用的工具类

    网上可以找到的mosquitto真的不多,俺这种不是什么技术大牛的人搞起来真的好费工夫。 在此本着共享精神跟大家分享。(其中的服务端的类基本上使用了...mosquitto_pub -h 192.168.4.71 -p 1883 -t KCBZ -m 您的库存不足

    linux-2.6-git.7z.002

    从http://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git下载

    titanic3.xls

    http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.xls, 有的电脑下载报错,网址打不开

    KittiSeg_pretrained.zip下载地址

    https://github.com/MarvinTeichmann/KittiSeg 运行demo.py时 需要下载KittiSeg_pretrained.zip,但是这个网站"ftp://mi.eng.cam.ac.uk/pub/mttt2/models/KittiSeg_pretrained.zip"总是抽风,所以上传了百度云盘一...

    泰坦尼克号旅客数据集

    泰坦尼克号旅客数据集titanic3.xls,原下载地址http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic3.xls无法获取到。网上的资源比较贵,不方便学习。现在将找到的titanic3.xls数据集以较低的分提供给...

    免授权脱单交友盲盒H5+小程序+APP源码下载

    源码介绍: 需要安装SG11应该是有加密,但是搭建过程中没有出现授权的情况,搭建是比较简单的,压缩包内有详细的安装教程。 可以打包小程序、H5、还有App(但是我这边测试App和H5都是没有小程序完善的, ...

    IARfor arm各版本官方下载地址

    - 下载地址:[http://files.iar.com/ftp/pub/box/CD-EWARM-6304-3295.zip](http://files.iar.com/ftp/pub/box/CD-EWARM-6304-3295.zip) - **V6.30.3 (CD-EWARM-6303-3241.zip)** - 下载地址:...

    MX OPC Server 6.04安装包以及安装教程

    百度网盘,https://pan.baidu.com/s/1o81Op844WXScvL3DSB4UNQ 提取码请见文件 安装教程请见b站博主 https://www.bilibili.com/video/BV1b44y1j7Tc?spm_id_from=333.880.my_history.page.click...一定要严格按照教程操作

    old version Object Arx SDK 下载地址

    - **下载地址**:`ftp://ftp.autodesk.com/pub/developer/sdk/obarxsdk.?exe` - **版本说明**:此版本适用于AutoCAD R14,是较早的一个版本,但仍然被许多开发者用于维护老项目。 2. **Object Arx SDK 2000** -...

    centos-yum离线源部署

    例如,将CentOS镜像挂载到`/var/ftp/pub/centos`,EPEL镜像挂载到`/var/ftp/pub/epel`。 ```bash mkdir /var/ftp/pub/epel mkdir /var/ftp/pub/centos mount /data/epel-7.x86_64.iso /var/ftp/pub/epel mount ...

    广东电信IPTV机顶盒 EC1308配置文件

    - **STB_model**: 设置为 `EC1308_pub` 该配置项指定了机顶盒的型号,这里是EC1308。 #### 2. SoftwareVersion 和 HWversion - **SoftwareVersion**: 设置为 `0.0.0.0` - **HWversion**: 设置为 `R002C01L01901` ...

    解决fatal:remote error:You can't push to git://github.com/username/*.git问题的办法

    3. **添加SSH公钥到GitHub**:找到生成的公钥文件(通常是`~/.ssh/id_rsa.pub`),将其内容复制到剪贴板。然后登录GitHub,进入个人设置 -&gt; SSH和GPG密钥,点击“新建SSH密钥”,输入一个标题(如“我的电脑”)并在...

    CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install ``` **2. 配置 Nginx** - 编辑 Nginx 的配置文件 `/usr/local/nginx/conf/nginx.conf`,...

    PHP微信支付WxPayPubHelper

    include_once("WxPay.pub.config.php"); /** * 所有接口的基类 */ class Common_util_pub { function __construct() { } function trimString($value) { $ret = null; if (null != $value) { $ret = $...

    Java帮助文档 JDK全系列 官方中英下载地址.docx

    下载链接:http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-windows-i586.exe 4. JDK帮助文档: - 对于学习和理解Java API,官方提供的帮助文档至关重要。Java 5、6和7的API文档都有英文和中文版本可供下载...

    https://pub.dev/packages/anim_search_bar-Flutter开发

    Anim搜索条形码参数width类型:double必需textController类型:TextEditingController必需onSuffixTap类型:函数su Anim搜索条形码参数width类型:double必需textController类型:TextEditingController必需...

Global site tag (gtag.js) - Google Analytics