Ruby/Reflection/kind of
Материал из Wiki.crossplatform.ru
determine our object"s class and its unique objectID and test its relationship to other classes.
num =1 num.id # 3 num.class # Fixnum num.kind_of? Fixnum # true num.kind_of? Numeric # true num.instance_of? Fixnum # true num.instance_of? Numeric # false
Return value based on parameter type
def coerce(other) if other.kind_of?(Float) return other, self.to_f elsif other.kind_of?(Integer) return other, self.to_i else super end end
use Object"s is_a? or kind_of? methods (they are synonyms)
myString = "asdf" puts myString.is_a? String # => true puts myString.kind_of? String # => true