2020-05-24 23:13:21 +05:30
|
|
|
# Newlines style guide
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
This style guide recommends best practices for newlines in Ruby code.
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
## Rule: separate code with newlines only to group together related logic
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
```ruby
|
|
|
|
# bad
|
|
|
|
def method
|
|
|
|
issue = Issue.new
|
|
|
|
|
|
|
|
issue.save
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
render json: issue
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# good
|
|
|
|
def method
|
|
|
|
issue = Issue.new
|
|
|
|
issue.save
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
render json: issue
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
## Rule: separate code and block with newlines
|
|
|
|
|
|
|
|
### Newline before block
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# bad
|
|
|
|
def method
|
|
|
|
issue = Issue.new
|
|
|
|
if issue.save
|
|
|
|
render json: issue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# good
|
|
|
|
def method
|
|
|
|
issue = Issue.new
|
|
|
|
|
|
|
|
if issue.save
|
|
|
|
render json: issue
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
## Newline after block
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# bad
|
|
|
|
def method
|
|
|
|
if issue.save
|
|
|
|
issue.send_email
|
|
|
|
end
|
|
|
|
render json: issue
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# good
|
|
|
|
def method
|
|
|
|
if issue.save
|
|
|
|
issue.send_email
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: issue
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
### Exception: no need for newline when code block starts or ends right inside another code block
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# bad
|
|
|
|
def method
|
|
|
|
|
|
|
|
if issue
|
|
|
|
|
|
|
|
if issue.valid?
|
|
|
|
issue.save
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# good
|
|
|
|
def method
|
|
|
|
if issue
|
|
|
|
if issue.valid?
|
|
|
|
issue.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|