-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kind_of? vs is_a? #307
Comments
|
Are you certain? The documentation appears to indicate that this behavior is identical: http://ruby-doc.org/core-2.1.1/Object.html#method-i-is_a-3F |
Hrm... I was certain, but it appears my memory has failed me. I ran into an issue a long time ago that caused me to prefer |
I guess someone should measure what's more popular in the wild. I have a feeling it will be |
The method 0.is_a? Fixnum Where the class name begins with a consonant sound, 0.kind_of? Array |
Comment from http://stackoverflow.com/questions/3893278/ruby-kind-of-vs-instance-of-vs-is-a#comment4147181_3893305
|
@NARKOZ I guess you meant to say the opposite. Even so, given Ruby's authors total disregard for rules of English such suggestions seem pretty far fetched. |
Has anyone looked into this further? |
I agree with the comment quoted by @sos4nt. It's the same as with |
I'd tend to agree with @fuadsaud (and hence presumably with @sos4nt). Given that, it's hard to make a rule that's both unambiguous and comprehensively correct, isn't it, and that would cause problems for automation. Not saying the Style Guide shouldn't say something about it, but enforcing the convention with RuboCop would be an interesting challenge, yes? |
I have looked into this more and the move I've looked the more I'm observing different projects using different rules to distinguish when one or the other is used. Sometimes there's no pattern (which is what bothered me initially into creating this). Given the validity of all the examples and rules brought forward in this issue, I agree it would be too hard to define a rule that would universally benefit people. |
@jdickey Doing a cop for that should be pretty straight-forward. Guess you can open a ticket so we won't forget about this. |
My understanding is that the
kind_of?
andis_a?
methods are exactly the same.I would like to propose defining a guideline for the use of theses two methods. I recently encountered code that used both of these in random places, and felt like it harmed the quality of the code. Upon consulting this guide I felt this was something that could be improved upon.
I would propose:
kind_of?
makes sense when extending classes:cat.kind_of?(Animal)
is_a?
is preferable with all built-in types:input.is_a?(Hash)
The text was updated successfully, but these errors were encountered: