# Ruby Style Guide
This is Airbnb's Ruby Style Guide.
It was inspired by [GitHub's guide](https://web.archive.org/web/20160410033955/https://github.com/styleguide/ruby) and [RuboCop's guide][rubocop-guide].
Airbnb also maintains a [JavaScript Style Guide][airbnb-javascript].
## Table of Contents
1. [Whitespace](#whitespace)
1. [Indentation](#indentation)
1. [Inline](#inline)
1. [Newlines](#newlines)
1. [Line Length](#line-length)
1. [Commenting](#commenting)
1. [File/class-level comments](#fileclass-level-comments)
1. [Function comments](#function-comments)
1. [Block and inline comments](#block-and-inline-comments)
1. [Punctuation, spelling, and grammar](#punctuation-spelling-and-grammar)
1. [TODO comments](#todo-comments)
1. [Commented-out code](#commented-out-code)
1. [Methods](#methods)
1. [Method definitions](#method-definitions)
1. [Method calls](#method-calls)
1. [Conditional Expressions](#conditional-expressions)
1. [Conditional keywords](#conditional-keywords)
1. [Ternary operator](#ternary-operator)
1. [Syntax](#syntax)
1. [Naming](#naming)
1. [Classes](#classes)
1. [Exceptions](#exceptions)
1. [Collections](#collections)
1. [Strings](#strings)
1. [Regular Expressions](#regular-expressions)
1. [Percent Literals](#percent-literals)
1. [Rails](#rails)
1. [Scopes](#scopes)
1. [Be Consistent](#be-consistent)
1. [Translation](#translation)
## Whitespace
### Indentation
* <a name="default-indentation"></a>Use soft-tabs with a
two-space indent.<sup>[[link](#default-indentation)]</sup>
* <a name="indent-when-as-case"></a>Indent `when` as deep as `case`.
<sup>[[link](#indent-when-as-case)]</sup>
```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
```
* <a name="align-function-params"></a>Align function parameters either all on
the same line or one per line.<sup>[[link](#align-function-params)]</sup>
```ruby
# bad
def self.create_translation(phrase_id, phrase_key, target_locale,
value, user_id, do_xss_check, allow_verification)
...
end
# good
def self.create_translation(phrase_id,
phrase_key,
target_locale,
value,
user_id,
do_xss_check,
allow_verification)
...
end
# good
def self.create_translation(
phrase_id,
phrase_key,
target_locale,
value,
user_id,
do_xss_check,
allow_verification
)
...
end
```
* <a name="indent-multi-line-bool"></a>Indent succeeding lines in multi-line
boolean expressions.<sup>[[link](#indent-multi-line-bool)]</sup>
```ruby
# bad
def is_eligible?(user)
Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
is_in_program?(user) &&
program_not_expired
end
# good
def is_eligible?(user)
Trebuchet.current.launch?(ProgramEligibilityHelper::PROGRAM_TREBUCHET_FLAG) &&
is_in_program?(user) &&
program_not_expired
end
```
### Inline
* <a name="trailing-whitespace"></a>Never leave trailing whitespace.
<sup>[[link](#trailing-whitespace)]</sup>
* <a name="space-before-comments"></a>When making inline comments, include a
space between the end of the code and the start of your comment.
<sup>[[link](#space-before-comments)]</sup>
```ruby
# bad
result = func(a, b)# we might want to change b to c
# good
result = func(a, b) # we might want to change b to c
```
* <a name="spaces-operators"></a>Use spaces around operators; after commas,
colons, and semicolons; and around `{` and before `}`.
<sup>[[link](#spaces-operators)]</sup>
```ruby
sum = 1 + 2
a, b = 1, 2
1 > 2 ? true : false; puts 'Hi'
[1, 2, 3].each { |e| puts e }
```
* <a name="no-space-before-commas"></a>Never include a space before a comma.
<sup>[[link](#no-space-before-commas)]</sup>
```ruby
result = func(a, b)
```
* <a name="spaces-block-params"></a>Do not include space inside block
parameter pipes. Include one space between parameters in a block.
Include one space outside block parameter pipes.
<sup>[[link](#spaces-block-params)]</sup>
```ruby
# bad
{}.each { | x, y |puts x }
# good
{}.each { |x, y| puts x }
```
* <a name="no-space-after-!"></a>Do not leave space between `!` and its
argument.<sup>[[link](#no-space-after-!)]</sup>
```ruby
!something
```
* <a name="no-spaces-braces"></a>No spaces after `(`, `[` or before `]`, `)`.
<sup>[[link](#no-spaces-braces)]</sup>
```ruby
some(arg).other
[1, 2, 3].length
```
* <a name="no-spaces-string-interpolation"></a>Omit whitespace when doing
string interpolation.<sup>[[link](#no-spaces-string-interpolation)]</sup>
```ruby
# bad
var = "This #{ foobar } is interpolated."
# good
var = "This #{foobar} is interpolated."
```
* <a name="no-spaces-range-literals"></a>Don't use extra whitespace in range
literals.<sup>[[link](#no-spaces-range-literals)]</sup>
```ruby
# bad
(0 ... coll).each do |item|
# good
(0...coll).each do |item|
```
### Newlines
* <a name="multiline-if-newline"></a>Add a new line after `if` conditions spanning
multiple lines to help differentiate between the conditions and the body.
<sup>[[link](#multiline-if-newline)]</sup>
```ruby
if @reservation_alteration.checkin == @reservation.start_date &&
@reservation_alteration.checkout == (@reservation.start_date + @reservation.nights)
redirect_to_alteration @reservation_alteration
end
```
* <a name="newline-after-conditional"></a>Add a new line after conditionals,
blocks, case statements, etc.<sup>[[link](#newline-after-conditional)]</sup>
```ruby
if robot.is_awesome?
send_robot_present
end
robot.add_trait(:human_like_intelligence)
```
* <a name="newline-different-indent"></a>Don’t include newlines between areas
of different indentation (such as around class or module bodies).
<sup>[[link](#newline-different-indent)]</sup>
```ruby
# bad
class Foo
def bar
# body omitted
end
end
# good
class Foo
def bar
# body omitted
end
end
```
* <a name="newline-between-methods"></a>Include one, but no more than one, new
line between methods.<sup>[[link](#newline-between-methods)]</sup>
```ruby
def a
end
def b
end
```
* <a name="method-def-empty-lines"></a>Use a single empty line to break between
statements to break up methods into logical paragraphs internally.
<sup>[[link](#method-def-empty-lines)]</sup>
```ruby
def transformorize_car
car = manufacture(options)
t = transformer(robot, disguise)
car.after_market_mod!
t.transform(car)
car.assign_cool_name!
fleet.add(car)
car
end
```
* <a name="trailing-newline"></a>End each file with a newline. Don't include
multiple newlines at the end of a file.
<sup>[[link](#trailing-newline)]</sup>
## Line Length
* Keep each line of code to a readable length. Unless
you have a reason not to, keep lines to fewer than 100 characters.
([rationale](./rationales.md#line-length
没有合适的资源?快使用搜索试试~ 我知道了~
ruby-main.zip
共70个文件
rb:43个
yml:16个
md:7个
需积分: 5 1 下载量 132 浏览量
2024-02-16
15:38:22
上传
评论
收藏 81KB ZIP 举报
温馨提示
Ruby 学习指南
资源推荐
资源详情
资源评论
收起资源包目录
ruby-main.zip (70个子文件)
ruby-main
LICENSE.md 1KB
.github
workflows
rspec_rubocop.yml 1KB
rationales.md 3KB
rubocop-airbnb
lib
rubocop
airbnb.rb 461B
airbnb
inject.rb 693B
inflections.rb 393B
rails_autoloading.rb 2KB
version.rb 155B
cop
airbnb
no_timeout.rb 784B
unsafe_yaml_marshal.rb 1KB
const_assigned_in_wrong_file.rb 3KB
factory_class_use_string.rb 1KB
class_or_module_declared_in_wrong_file.rb 4KB
simple_unless.rb 544B
opt_arg_parameters.rb 1KB
continuation_slash.rb 726B
rspec_environment_modification.rb 2KB
rspec_describe_or_context_under_namespace.rb 4KB
phrase_bundle_keys.rb 2KB
module_method_in_wrong_file.rb 3KB
simple_modifier_conditional.rb 735B
spec_constant_assignment.rb 1KB
risky_activerecord_invocation.rb 2KB
default_scope.rb 699B
mass_assignment_accessible_modifier.rb 522B
factory_attr_references_class.rb 3KB
rubocop-airbnb.rb 262B
.rubocop.yml 132B
LICENSE.md 1KB
.rspec 30B
rubocop-airbnb.gemspec 1KB
spec
rubocop
cop
airbnb
continuation_slash_spec.rb 3KB
default_scope_spec.rb 888B
risky_activerecord_invocation_spec.rb 1KB
factory_attr_references_class_spec.rb 4KB
simple_unless_spec.rb 673B
rspec_environment_modification_spec.rb 1KB
simple_modifier_conditional_spec.rb 3KB
no_timeout_spec.rb 819B
factory_class_use_string_spec.rb 555B
class_or_module_declared_in_wrong_file_spec.rb 5KB
phrase_bundle_keys_spec.rb 1KB
mass_assignment_accessible_modifier_spec.rb 706B
opt_arg_parameter_spec.rb 2KB
module_method_in_wrong_file_spec.rb 4KB
spec_constant_assignment_spec.rb 2KB
const_assigned_in_wrong_file_spec.rb 4KB
unsafe_yaml_marshal_spec.rb 924B
rspec_describe_or_context_under_namespace_spec.rb 6KB
spec_helper.rb 1KB
CHANGELOG.md 2KB
.rubocop_airbnb.yml 385B
Gemfile 71B
README.md 2KB
config
rubocop-bundler.yml 128B
rubocop-style.yml 29KB
default.yml 1KB
rubocop-airbnb.yml 3KB
rubocop-rails.yml 5KB
rubocop-security.yml 322B
rubocop-rspec.yml 8KB
rubocop-performance.yml 4KB
rubocop-naming.yml 2KB
rubocop-metrics.yml 1KB
rubocop-layout.yml 16KB
rubocop-lint.yml 8KB
rubocop-gemspec.yml 292B
CONTRIBUTING.md 1KB
.gitignore 314B
README.md 46KB
共 70 条
- 1
资源评论
徐浪老师
- 粉丝: 6290
- 资源: 6880
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- python 数据分析示例
- tensorflow-gpu-2.5.3-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-gpu-2.2.1-cp35-cp35m-win-amd64.whl
- tensorflow-gpu-2.5.3-cp38-cp38-manylinux2010-x86-64.whl
- 2023-04-06-项目笔记 - 第一百三十五阶段 - 4.4.2.133全局变量的作用域-133 -2024.05.16
- Screenshot_20240516_224007.jpg
- IEEE802.3dj-800G DR4 & 1.6T DR8-welch-3dj-01a-230206 .pdf
- Matlab三维图绘制基础了解
- IEEE802.3dj-800GBASE FR4-welch-3dj-01a-2401 .pdf
- 实验3-拆弹专家-3023244203-刘宇翔
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功