gpt4 book ai didi

ruby - 检查ruby中实例的类型

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:08 24 4
gpt4 key购买 nike

我正在调试一个应用程序,我需要在其中检查 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 不起作用。我该如何检查?

最佳答案

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

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

关于ruby - 检查ruby中实例的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24377289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com