What is the difference? When should I use which? Why are there so many of them?
-
23As to why both `is_a?` and `kind_of?` exist: I suppose it's part of Ruby's design philosophy. Python would say there should only be one way to do something; Ruby often has synonymous methods so you can use the one that sounds better. It's a matter of preference. It may partly be due to Japanese influence: I'm told that they will use a different word for the same number depending on the sentence in order to make it sound nicer. Matz may have carried that idea into his language design. – Nathan Long Mar 21 '11 at 16:04
-
@NathanLong I don't think Japanese counters have much to do with it; all languages have some sort of agreement and you can't go substituting one counter for another most of time (like, you can't use the cylinder counter for flat objects; it's just wrong). And it has more to do with semantics than euphony. – Casey Aug 03 '16 at 14:38
-
5@Casey, Nathan is probably referring to 四 (four), which can be pronounced both "shi" and "yon". Japanese speakers will sometimes avoid the "shi" pronunciation because it's a homophone with (sounds the same as) 死 (death). see [this omniglot article](https://www.omniglot.com/language/numbers/japanese.htm) for example: « The numbers 4 and 9 are considered unlucky in Japanese: 4, when pronounced shi, sounds like the word for death (死), and 9, when pronounced ku, sounds like the word for suffering (苦). So they are often pronounced yon and kyu instead. » – RubyTuesdayDONO Feb 19 '21 at 05:11
-
@RubyTuesdayDONO It's not really about avoiding that association; in many situations only one or the other is correct and you can't substitute willy-nilly. – Casey Feb 19 '21 at 16:33
5 Answers
kind_of?
and is_a?
are synonymous.
instance_of?
is different from the other two in that it only returns true
if the object is an instance of that exact class, not a subclass.
Example:
"hello".is_a? Object
and"hello".kind_of? Object
returntrue
because"hello"
is aString
andString
is a subclass ofObject
.- However
"hello".instance_of? Object
returnsfalse
.
-
-
91It just reads better sometimes. Think `@honda.kind_of? Car` and `@person.is_a? Administrator`, Ruby's all about the aesthetics. In fact, notice the grammatical error... with active support you can write `@person.is_an? Administrator` :)... That might have made it into Ruby core by now, actually. – rfunduk Oct 08 '10 at 19:18
-
3heh that's an interesting reason. can you break this, thouugh? like can you override `kind_of?` but not `is_a?`? – Claudiu Oct 08 '10 at 19:38
-
4@thenduks, `is_an?` is not in ruby-1.9.2-p0. @Claudiu, no. [`is_a?` is just an alias of `kind_of?`](https://github.com/ruby/ruby/blob/trunk/object.c#L2582). Both methods invoke the same c function, [`rb_obj_is_kind_of`](https://github.com/ruby/ruby/blob/trunk/object.c#L471). – ma11hew28 Feb 07 '11 at 05:57
-
10@Matt: You can override an alias without overriding the aliased function. So yes, you can override `kind_of?` without overriding `is_a?`. – sepp2k Feb 07 '11 at 06:01
-
@Claudiu Ruby's core and standard library has historically had multiple objects whose methods may be actually aliases of another method. Sometimes, I believe it was for the ability to express yourself in something close to natural language. Backwards compatibility has likely also been a factor. – SpaceGhost Jan 15 '13 at 21:56
-
5Where is this ActiceSupport `is_an?` method?! It's not in the current rails version, and I can't find anything on google about it being deprecated either. – Tom Lord Jan 30 '15 at 16:39
-
@Joel Both `kind_of?` and `is_a?` match subclasses. Only `instance_of?` accepts the exact class only. – sepp2k Nov 27 '15 at 13:29
-
Ruby 2.4 unifies `Fixnum` and `Bignum` into one class (`Integer`) - see https://www.ruby-lang.org/en/news/2016/06/20/ruby-2-4-0-preview1-released/ . Might be a good idea to update the example (e.g., `5.is_a? Object` returns true but `5.instance_of? Object` return false). – dubek Sep 13 '16 at 10:16
-
-
@LokanadhamMotumarri Yes, `SomeClassOrModule === some_object` is equivalent to `some_object.is_a? SomeClassOrModule`. – sepp2k Feb 19 '19 at 22:19
What is the difference?
From the documentation:
- - (Boolean)
instance_of?(class)
- Returns
true
ifobj
is an instance of the given class.
and:
- - (Boolean)
is_a?(class)
- (Boolean)kind_of?(class)
- Returns
true
ifclass
is the class ofobj
, or ifclass
is one of the superclasses ofobj
or modules included inobj
.
If that is unclear, it would be nice to know what exactly is unclear, so that the documentation can be improved.
When should I use which?
Never. Use polymorphism instead.
Why are there so many of them?
I wouldn't call two "many". There are two of them, because they do two different things.

- 1
- 1

- 363,080
- 75
- 446
- 653
-
5I think my confusion was that there are 3, and that 2 just do the same thing and have different names. About using polymorphism - I agree, but the ruby standard library is full of uses of each of these – Claudiu Oct 08 '10 at 18:56
-
4
-
2
-
3It is often better to do polymorphism, yes, but there are border cases where you really want to know that you have a specific class, such as when you are dealing with files. – Automatico Jan 16 '15 at 12:32
It is more Ruby-like to ask objects whether they respond to a method you need or not, using respond_to?
. This allows both minimal interface and implementation unaware programming.
It is not always applicable of course, thus there is still a possibility to ask about more conservative understanding of "type", which is class or a base class, using the methods you're asking about.

- 158,662
- 42
- 215
- 303

- 313
- 4
- 6
-
9It depends on situation. Both Comment and Blog may respond to created_at. In such situation is_a? is more appropriate IMHO – penkovsky May 17 '13 at 15:30
-
That doesn't make sense, if you needed to distinguish a Comment and a Blog object from each other, you simply wouldn't use created_at to do it. That doesn't preclude that you could write a method which takes an object that responds to created_at. If it doesn't need anything else to do its job, then you could safely use it on Comment or Blog, or fairly any other ActiveRecord model. – Kingdon Jul 02 '19 at 16:45
I also wouldn't call two many (is_a?
and kind_of?
are aliases of the same method), but if you want to see more possibilities, turn your attention to #class
method:
A = Class.new
B = Class.new A
a, b = A.new, B.new
b.class < A # true - means that b.class is a subclass of A
a.class < B # false - means that a.class is not a subclass of A
# Another possibility: Use #ancestors
b.class.ancestors.include? A # true - means that b.class has A among its ancestors
a.class.ancestors.include? B # false - means that B is not an ancestor of a.class

- 12,444
- 5
- 57
- 74
-
1Thanks - i was indeed asking in a general sense of "what run-time type information can be gathered in Ruby and how" - and this provides ample examples – Claudiu Jun 12 '13 at 01:20
-
https://stackoverflow.com/a/3893305/10392483 is a great explanation ... to add some more colour to this, I tend to use is_a?
for "primatives" (String, Array, maybe Hash, etc.)
So "hello".is_a?(String)
, [].is_a?(Array)
, {}.is_a?(Hash)
For anything else, I tend to use instance_of? (Animal.new.instance_of?(Animal)
I say tend to because it's not quite that clear cut. Take for example:
class Animal;end
class Dog < Animal;end
x = Dog.new
x.is_a?(Dog) # => true
x.is_a?(Animal) # => true
x.instance_of?(Dog) # => true
x.instance_of?(Animal) # => false
As you can see, x is both a Dog and an Animal, but it's only an instance of Dog.
I see it as a question of specificity:
- If I just want to know that it's an
Animal
and not aPlant
I'll useis_a?
- If I care that it's a
Dog
and not aCat
I'll useinstance_of?
You can then take this further. If I care that it's a Sighthound
and not a Bloodhound
, assuming both are subclasses of Dog
. Then I may want to make it even more specific.
That said, is_a?(Animal|Dog|Sighthound)
will always work. But if you care about the specific subclass, instance_of?
is always more specific.

- 2,458
- 10
- 44
- 49