rubocop / rubocop/ruby-style-guide

Additional thoughts on `self`

Open
#666 2 comments 1 reaction 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

The style guide currently takes a hard-line position on self that it shouldn't be used unless you're doing an attribute assignment. I would argue that this is actually an over-simplification.

Consider:

  • self needs to be used with operators (e.g. self + other, self - [:x, :y], depends on the domain). In fact, RuboCop even calls this out explicitly as a case where self is not penalized because it's required.
  • clarity in "spaceship operator" comparisons (i.e. sorting methods).

For example:

class Student
  def <=>(other)
    self.name <=> other.name
  end
end

In this case, self helps draw a distinction between a value in the current instance and the other instance. It balances out the comparison.

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 by reading the style guide's current guidance on self and the linked RuboCop explanation of required uses. Review the supplied operator and spaceship-comparison examples, then determine with maintainers whether the rule should be revised and what wording and examples would define the completed documentation change.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.