rubocop / rubocop/ruby-style-guide

Constants vs. class methods

Open
#328 16 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Greetings,
I would like to hear some community standpoint regarding this matter.

class User < ActiveRecord::Base
  ROLES = %w(admin moderator contributor)
end

# > User::ROLES
# => ["admin", "moderator", "contributor"]

or...

class User < ActiveRecord::Base
  self.roles
    @roles ||= %w(admin moderator contributor)
  end
end

# > User.roles
# => ["admin", "moderator", "contributor"]

I personally prefer class methods since this approach looks more object-oriented to me while screaming snake cased constant feels kinda ugly typographically.

There is also interesting Stack Overflow discussion.

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 comparing the two Ruby examples in the issue and reading the linked Stack Overflow discussion. The task is to establish a project style-guide position on constants versus class methods; it is done when the community reaches a decision and the guidance is recorded in the style guide.

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
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.