principlerubyCritical
Ruby: kind_of? vs. instance_of? vs. is_a?
Viewed 0 times
kind_ofrubyinstance_ofis_a
Problem
What is the difference? When should I use which? Why are there so many of them?
Solution
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? Objectand"hello".kind_of? Objectreturntruebecause"hello"is aStringandStringis a subclass ofObject.
- However
"hello".instance_of? Objectreturnsfalse.
Context
Stack Overflow Q#3893278, score: 733
Revisions (0)
No revisions yet.