Ruby에 kind_of? / instance_of? / is_a? 의 차이점
조회수 3245회
1 답변
-
kind_of?
와is_a?
는 같습니다.kind_of
/is_a
class
가obj
의 클래스인 경우, 혹은class
가obj
의 superclass거나ㅓobj
에 포함되어 있는 모듈인 경우 true를 return합니다.instance_of?
객체가 정확히 해당 클래스의 instance일때만
true
를 return합니다. (상속에서도false
)예를들면
puts 5.is_a? Integer #true puts 5.kind_of? Integer #true puts 5.instance_of? Integer #false
출처 - ruby-doc :
kind_of?
- kind_of?(class) → true or false
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.
is_a?
- is_a?(class) → true or false
Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.
instance_of?
- instance_of?(class) → true or false
Returns true if obj is an instance of the given class. See also Object#kind_of?.
댓글 입력