528
votes

What is the difference? When should I use which? Why are there so many of them?

4
As 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
@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
@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 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
@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

4 Answers

668
votes

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 return true because "hello" is a String and String is a subclass of Object.
  • However "hello".instance_of? Object returns false.
23
votes

What is the difference?

From the documentation:

- (Boolean) instance_of?(class)
Returns true if obj is an instance of the given class.

and:

- (Boolean) is_a?(class)
- (Boolean) kind_of?(class)
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

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.

9
votes

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.

4
votes

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