检查ruby中的实例类型

时间:2014-06-24 02:06:05

标签: ruby

我正在调试一个应用程序,我需要检查DNS查询的资源类是否为IN类型。这是我发现的:

[6] pry(#<DNS::Server>)> resource_class
=> Resolv::DNS::Resource::IN::A
[7] pry(#<DNS::Server>)> resource_class.inspect
=> "Resolv::DNS::Resource::IN::A"
[8] pry(#<DNS::Server>)> resource_class.class.name
=> "Class"
[9] pry(#<DNS::Server>)> resource_class.kind_of? Class
=> true
[10] pry(#<DNS::Server>)> resource_class.kind_of? Resolv::DNS::Resource::IN::A
=> false
[11] pry(#<DNS::Server>)> resource_class.instance_of? Class
=> true
[12] pry(#<DNS::Server>)> resource_class.instance_of? Resolv::DNS::Resource::IN::A
=> false
[13] pry(#<DNS::Server>)> resource_class.is_a? Class
=> true
[14] pry(#<DNS::Server>)> resource_class.is_a? Resolv::DNS::Resource::IN::A
=> false

我试图使用无效的resource_class.instance_of? Resolv::DNS::Resource::IN::A进行检查。我该如何检查?

1 个答案:

答案 0 :(得分:3)

您的resource_class是一个班级。它不是Resolv::DNS::Resource::IN::A的实例;它 Resolv::DNS::Resource::IN::A。因此,简单的比较就足够了:

resource_class == Resolv::DNS::Resource::IN::A