我从API中收到了一个似乎包含的数组,因为缺少更好的短语,对象式符号。
由于它是一个数组,因此我显然无法通过键或值访问它。我在此特定对象类型中没有看到一种方法,它允许我将其转换为哈希,然后将其转换为哈希。
。将其转换为哈希的最合适的方法是我可以按键和值访问数据的方法?
输出:
[#<ObjectType::ObjectRef:0x30f2862
@_ref="record:full/data/location/setting/partition",
@configured=false,
@name="John Doe",
@addr="10.10.10.10">]
另外,如果可以将其转换为具有多个元素的数组(而不是一个大块的元素),我可以将其解析为CSV并在Ruby外面过滤。新数组中的元素将包含"值"(例如False," John Doe"," 10.10.10.10)。
尝试以下:
array # => [#<ObjectType::ObjectRef:0x30f2862
@_ref="record:full/data/location/setting/partition",
@configured=false,
@name="John Doe",
@addr="10.10.10.10">]
array.map { |a| {configured: a.configured, name: a.name, addr: a.addr} }
# or if you can't access the instance variables
array.map do |a|
{
configured: a.instance_variable_get(:@configured),
name: a.instance_variable_get(:@name),
addr: a.instance_variable_get(:@addr)
}
end # => [{configured: false, name: "John Doe", addr: "10.10.10.10"}]
# and if you want an array
array.map do |a|
[
a.instance_variable_get(:@configured),
a.instance_variable_get(:@name),
a.instance_variable_get(:@addr)
]
end # => [[false, "John Doe", "10.10.10.10"]]