Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better clarity on case-when indentation #741

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ Translations of the guide are available in the following languages:
puts 'Not again!'
when song.duration > 120
puts 'Too long!'
when Time.now.hour > 21
puts "It's too late"
when :minus_op, :minus_minus_op
stack.pop - stack.pop
when :int_literal, :some_complicate_explicit_name, :contains_musicians_with_arms, :str_interpolated
token.value
else
song.play
end
Expand All @@ -346,6 +348,25 @@ Translations of the guide are available in the following languages:
end
```

Put multiple when conditions on separate lines,
particularly where the conditions form long, complicated lines.
The 'bad' example also has an issue with code diffs, causing the entire line to
diff when only one of the conditions is changed or updated.


```
# better (for multi-condition)
case
when :minus_op, :minus_minus_op
stack.pop - stack.pop
when :int_literal,
:some_complicate_explicit_name,
:contains_musicians_with_arms,
:str_interpolated
token.value
end
```

* <a name="indent-conditional-assignment"></a>
When assigning the result of a conditional expression to a variable,
preserve the usual alignment of its branches.
Expand Down