开发者

Retrieve the "kind" of a Class and Class Instance

I am using Ruby on Rails 3.0.9 and I would like to check if a object is a class or a class instance. For example if I have

Article  # It is a class name
@arti开发者_开发知识库cle # It is an instance of the Article class

maybe I may do something like the following:

kind?(Article)  # => class
kind?(@article) # => class_instance

How can I retrieve that information?


Object has a method called class:

@article.class # => Article

There's also kind_of?:

if @article.kind_of? Class
   # class type
elsif @article.kind_of? Article
   # other type
end


Class is an object of class Class:

class A
end

Class === A       #=> true
Class === A.new   #=> false
A === A.new       #=> true

A.new here is an object of class A

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜