- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在Ruby 中,是否可以使用任何方法来识别对象o 在类层次结构中是否具有类C 作为其祖先?
我在下面给出了一个示例,其中我使用假设方法 has_super_class?
来完成它。实际应该怎么做?
o = Array.new
o[0] = 0.5
o[1] = 1
o[2] = "This is good"
o[3] = Hash.new
o.each do |value|
if (value.has_super_class? Numeric)
puts "Number"
elsif (value.has_super_class? String)
puts "String"
else
puts "Useless"
end
end
预期输出:
Number
Number
String
Useless
最佳答案
尝试obj.kind_of?(类名)
:
1.kind_of?(Fixnum) => true
1.kind_of?(Numeric) => true
....
1.kind_of?(Kernel) => true
kind_of?
方法也有一个相同的替代方法 is_a?
。
如果你只想检查一个对象是否是一个类的(直接)实例,使用obj.instance_of?
:
1.instance_of?(Fixnum) => true
1.instance_of?(Numeric) => false
....
1.instance_of?(Kernel) => false
您还可以通过在其类
上调用ancestors
方法来列出对象的所有祖先。例如 1.class.ancestors
给你 [Fixnum, Integer, Precision, Numeric, Comparable, Object, PP::ObjectMixin, Kernel]
。
关于Ruby:我们如何识别对象 o 在类层次结构中是否有类 C 作为其祖先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285001/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!