[Ruby] Difference between is_a? And instance_of?

Try it on your Mac

irb

Premise

module ParentModule; end
module ChildModule; end

class ParentClass
  include ParentModule
end

class ChildClass < ParentClass
  include ChildModule
end

child_instance = ChildClass.new

is_a? method

child_instance.is_a? ChildClass #=> true
child_instance.is_a? ParentClass #=> true
child_instance.is_a? Object #=> true
child_instance.is_a? String #=> false
child_instance.is_a? ParentModule #=> true
child_instance.is_a? ChildModule #=> true

--A direct instance of the class, or --An instance of that subclass, or --A class that includes a module, or a class --Returns true if it is an instance of that subclass (The meaning of is_a may be the meaning of is one of ~)

instance_of? method

child_instance.instance_of? ChildClass #=> true
child_instance.instance_of? ParentClass #=> false
child_instance.instance_of? Object #=> false
child_instance.instance_of? String #=> false
child_instance.instance_of? ParentModule #=> false
child_instance.instance_of? ChildModule #=> false

Recommended Posts

[Ruby] Difference between is_a? And instance_of?
[Ruby] Difference between get and post
Note: Difference between Ruby "p" and "puts"
Difference between Ruby instance variable and local variable
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Difference between print, puts and p
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] Difference between puts and return, output and return value
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
[Ruby] Difference between match / scan
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Ruby] I thought about the difference between each_with_index and each.with_index
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
About the difference between classes and instances in Ruby
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
About the difference between "(double quotation)" and "single quotation" in Ruby
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
The difference between programming with Ruby classes and programming without it
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Easy to understand the difference between Ruby instance method and class method.
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update