A method that checks the element type and returns true if it matches the specified type, false if it is different,
About the difference between is_a?, kind_of?, Instance_of?.
is_a? and kind_of? are the sameis_a? and instance_of?is_a?· Receiver method (type)
Returns true if the receiver matches the type specified in the argument, false otherwise.
Example (is_a?)
arr = [1, 2, 3]
arr.is_a?(Array)
=> true
arr.is_a?(String)
=> false
Example (kind_of?)
arr = [1, 2, 3]
arr.kind_of?(Array)
=> true
arr.kind_of?(String)
=> false
Example (instance_of?)
arr = [1, 2, 3]
arr.instance_of?(Array)
=> true
arr.instance_of?(String)
=> false
Illustration
def _article_items
if article_items.is_a?(Hash)
[
article_items.fetch('default')
]
elsif article_items.is_a?(Array)
article_items
else
[]
end
end
| Element example | Type (class) |
|---|---|
| "string" | String |
| 100 | Integer |
| 1.23 | Floot |
| [1, 2, 3] | Array |
| 1..3 | Range |
| {:a=>1, :b=>2, :c=>3} | Hash |
| class | Class |
| module | Module |
-Instance_of?: Evaluate only the receiver
・ Is_a?: Superclass type is also evaluated
A superclass is the parent class of the current element's class.
is_a? returns true even if the argument contains a superclass type.
An example is as follows. (Details of superclass will be described later)
python
arr = [1, 2, 3]
arr.is_a?(Object)
=> true
arr.instance_of?(Object)
=> false
When Object, which is a superclass of Array, is used as an argument, is_a? is true and instance_of? is false.
Each element has a class, and the parent class of that class is the superclass.
For example
--Integer 1 class (type) is Integer. --The Integer superclass is Numeric, which represents all numbers. --Numeric's superclass is an Object that represents all the data. --The Object superclass is a BasicObject that has only basic functions such as identification and method specification. This is the highest class.
Hierarchical structure of classes
BasicObject
↓
Object
↓
String, Numeric, Module, Class,,,
↓
Integer, Floot
python
BasicObject.superclass
=> nil
| element | Type (class) | Super class | Super class super class |
|---|---|---|---|
| "string" | String | Object | Basic Object |
| 100 | Integer | Numeric | Object |
| 1.23 | Floot | Numeric | Object |
| [1, 2, 3] | Array | Object | Basic Object |
| 1..3 | Range | Object | Basic Object |
| {:a=>1, :b=>2, :c=>3} | Hash | Object | Basic Object |
| class | Class | Object | Basic Object |
| module | Module | Object | Basic Object |
| nil | NilClass | Object | Basic Object |
** ▼ Caution 1 **
Note that it will be true even if you specify a superclass as an argument, so if you specify an Object or Basic Object that corresponds to all the data, true will be returned.
python
obj = {:a=>1, :b=>2, :c=>3}
obj.is_a?(BasicObject)
=> true
100.is_a?(BasicObject)
=> true
"String".is_a?(BasicObject)
=> true
python
class Aaa
end
##Class inheritance
class Bbb < Aaa
end
##Superclass class name is false
Bbb.is_a?(Aaa)
=> false
##If you specify the type, true is returned
Bbb.is_a?(Class)
=> true
Recommended Posts