主页 > 知识库 > 网络编程 > Ruby >

Ruby 编程风格介绍

来源:ruby-china 作者:网络 发表于:2012-06-20 09:50  点击:
本页用于介绍 Ruby 社区首推的代码编写风格,本文翻译来自: https://github.com/bbatsov/ruby-style-guide 目录 格式,文件格式 语法 命名 注释 注解 类相关 异常 集合 字符串 正则表达式 百分号 元编程 杂项 The Ruby Style Guide This Ruby style guide re
本页用于介绍 Ruby 社区首推的代码编写风格,本文翻译来自: https://github.com/bbatsov/ruby-style-guide

目录

  • 格式,文件格式
  • 语法
  • 命名
  • 注释
  • 注解
  • 类相关
  • 异常
  • 集合
  • 字符串
  • 正则表达式
  • 百分号
  • 元编程
  • 杂项

The Ruby Style Guide

This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers. A style guide that reflects real-world usage gets used, and a style guide that holds to an ideal that has been rejected by the people it is supposed to help risks not getting used at all – no matter how good it is.
The guide is separated into several sections of related rules. I've tried to add the rationale behind the rules (if it's omitted I've assumed that is pretty obvious).
I didn't come up with all the rules out of nowhere - they are mostly based on my extensive career as a professional software engineer, feedback and suggestions from members of the Ruby community and various highly regarded Ruby programming resources, such as "Programming Ruby 1.9" and "The Ruby Programming Language".
The guide is still a work in progress - some rules are lacking examples, some rules don't have examples that illustrate them clearly enough. In due time these issues will be addressed - just keep them in mind for now.

格式, 文件格式

  • 使用 UTF-8 作为文件的编码。
  • 缩进使用 两个空格,别用 Tab 格式,Tab将会导致到了其他环境排版混乱。
  • 使用 Unix 的换行格式, (Linux/OSX 用户默认就是这样的,Windows 用户这里要多加注意)
    • 如果你在 Windows 下面,并且使用 Git,可以用下面的方式做调整:
      $ git config --global core.autocrlf true
      
  • 在逗号,分号,冒号,运算符号的左右留下一个空格。
    sum = 1 + 2
    a, b = 1, 2
    1 > 2 ? true : false; puts 'Hi'
    [1, 2, 3].each { |e| puts e }
    
唯一的例外是当你在做指数运算的时候,比如:
```ruby
# 不好的
e = M * c ** 2

# 好的
e = M * c**2
```
  • (, [ 的后面,或 ], ) 的前面别敲空格。
    some(arg).other
    [1, 2, 3].length
    
  • whencase相同缩进。我知道很多人不赞同这点,但这是 "The Ruby Programming Language" and "Programming Ruby"两本书共同沿袭的风格。
    case
    when song.name == 'Misty'
      puts 'Not again!'
    when song.duration > 120
      puts 'Too long!'
    when Time.now.hour > 21
      puts "It's too late"
    else
      song.play
    end
    
    kind = case year
           when 1850..1889 then 'Blues'
           when 1890..1909 then 'Ragtime'
           when 1910..1929 then 'New Orleans Jazz'
           when 1930..1939 then 'Swing'
           when 1940..1950 then 'Bebop'
           else 'Jazz'
           end
    
  • 用空行来分割def以及方法中的逻辑段。
    def some_method
      data = initialize(options)
    
      data.manipulate!
    
      data.result
    end
    
    def some_method
      result
    end
    
  • 如果方法的参数出现在多行,则将它们对齐。
    # 开始时是这样(太长了)
    def send_mail(source)
      Mailer.deliver(to: 'bob@example.com', from: 'us@example.com', subject: 'Important message', 
    body: source.text)
    end
    
    # 不好的(普通缩进)
    def send_mail(source)
      Mailer.deliver(
        to: 'bob@example.com',
        from: 'us@example.com',
        subject: 'Important message',
        body: source.text)
    end
    
    # 不好的(双倍缩进)
    def send_mail(source)
      Mailer.deliver(
          to: 'bob@example.com',
          from: 'us@example.com',
          subject: 'Important message',
          body: source.text)
    end
    
    # 好的
    def send_mail(source)
      Mailer.deliver(to: 'bob@example.com',
                     from: 'us@example.com',
                     subject: 'Important message',
                     body: source.text)
    end
    
  • 使用RDoc以及它的约定来写API文档,并且不要在注释块与def中加入空行。*
  • 保持一行的长度小于80个字符。*
    • Emacs用户可以将如下内容加入配置中*

      (e.g. ~/.emacs.d/init.el):
      (setq whitespace-line-count 80
            whitespace-style '(lines))
      `    ``
      
  • Vim用户可以将如下内容加入配置中
    (e.g. ~/.vimrc):
    ```vim
    " VIM 7.3+ has support for highlighting a specified column.
    if exists('+colorcolumn')
        set colorcolumn=80
    else
        " Emulate
        au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%80v.\+', -1)
    endif
    ```
    
                

有帮助
(0)
0%
没帮助
(0)
0%