rubocop / rubocop/ruby-style-guide

Unspecified Style point: obj.is_a? vs obj.class == Class

Open
#416 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Although the case equality operator === is mentioned, of the two methods of class checking demonstrated here:

"this string".is_a? String  # => true
"this string".is_a? Object  # => true
4.is_a? Object              # => true

"this string".class == CSV  # => false
4.class == Fixnum           # => true
4.class == String           # => false

Neither are stated as preferred. I think it's fairly well agreed that is_a? is the the more Ruby way of doing things, but I've seen obj.class == Classname fairly often as well and it's easily readable.

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

The issue names no file or test; start by locating the style-guide section containing these class-checking examples and review the existing discussion. Done means the guide explicitly states whether is_a? or obj.class == Classname is preferred, or clearly documents when each is appropriate.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.