rubocop / rubocop/ruby-style-guide

Validity of single line while/until rule

Open
#634 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Approved Existing Rule
Dominant language
No language data
Stars
16.5k
Forks
3.3k
PR merge metrics
No merged PRs in 30d

Description

Referring to this: https://github.com/bbatsov/ruby-style-guide#while-as-a-modifier

I would argue that the single line version of the syntax is a rare example of ruby being surprising and inconsistent.

We all regularly use something like

foo = bar unless bar.nasty?

This is nice. It's clear that foo = bar is the last line run if it gets run at all. If this is at the end of a method for some silly reason you're safe knowing bar will be returned under the right circumstances.

but try this to fix your nasty situation:

bar = bar.next_sibling while bar.nasty?

foo = bar.next is not the real last line run. It only looks like it. In fact the last operation is the close of the while loop which returns nil.

For this reason I prefer to keep that explicit with

while bar.nasty?
  bar = bar.next_sibling
end

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Ruby style guide section, “while as a modifier,” and compare its current guidance with the single-line examples in this issue. Done means the guide clearly resolves whether the single-line while/until form should be discouraged in favor of an explicit loop, with the rationale and examples aligned.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.