Skip to content

Commit 2186b71

Browse files
author
Zachary Jones
committed
add style for multi-condition when-then blocks
1 parent bdde52a commit 2186b71

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,54 @@ Translations of the guide are available in the following languages:
326326
end
327327
```
328328

329+
* <a name="multi-condition-case-when"></a>
330+
Put multiple when conditions on separate lines.
331+
Particularly where the conditions form long, complicated lines.
332+
<sup>[[link](#multi-condition-case-when)]</sup>
333+
334+
```Ruby
335+
# good
336+
337+
case token
338+
when :star_op
339+
stack.pop * stack.pop
340+
when :minus_op, :minus_minus_op
341+
also_calculate_that
342+
stack.pop - stack.pop
343+
when MyModule::SomeDomain::BETA_USERS,
344+
MyModule::SomeDomain::INTERNAL_RELEASE
345+
stack.pop + stack.pop
346+
when :int_literal,
347+
:some_complicate_explicit_name,
348+
:graduate_borrowers_with_arms,
349+
:str_interpolated
350+
token.value
351+
end
352+
```
353+
354+
Though better control of primary domain references should be exercised, this style offers a solution for some 'in the wild' situations. It reads better than:
355+
356+
```Ruby
357+
# bad
358+
359+
case token
360+
when :star_op
361+
stack.pop * stack.pop
362+
when :slash_op
363+
stack.pop / stack.pop
364+
when :minus_op, :minus_minus_op
365+
also_calculate_that
366+
stack.pop - stack.pop
367+
when MyModule::SomeDomain::BETA_USERS, MyModule::SomeDomain::INTERNAL_RELEASE
368+
stack.pop + stack.pop
369+
when :int_literal, :some_complicate_explicit_name, :graduate_borrowers_with_arms, :str_interpolated
370+
token.value
371+
end
372+
```
373+
374+
The 'bad' example also has the issue of cause the entire when line to diff when one of the conditions is changed or updated
375+
376+
329377
* <a name="indent-conditional-assignment"></a>
330378
When assigning the result of a conditional expression to a variable,
331379
preserve the usual alignment of its branches.

0 commit comments

Comments
 (0)