ruby - Ruby 数据结构@?
问题描述
如何@hostname
从这些数据中提取价值?例如做我所期待的puts output[:hostname]
{:status=>"passed",
:code_desc=>"Host example.com port 80 proto tcp is expected to be reachable",
:run_time=>0.207583,
:start_time=>"2020-08-27T21:02:07+01:00",
:resource_title=>
#<#<Class:0x00007f87f71ebeb0>:0x00007f87f71eaf38
@__backend_runner__=
Inspec::Backend::Class @transport=Train::Transports::Local::Connection,
@__resource_name__=:host,
@host_provider=
#<Inspec::Resources::DarwinHostProvider:0x00007f87f71e9a20
@has_nc=true,
@has_ncat=true,
@has_net_redirections=true,
@inspec=
Inspec::Backend::Class @transport=Train::Transports::Local::Connection>,
@hostname="example.com",
@ip_cache=["93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946"],
@ping_cache=
{:success=>true,
:connection=>"Connection to example.com port 80 [tcp/http] succeeded!\n",
:socket=>""},
@port=80,
@protocol="tcp",
@resource_exception_message=nil,
@resource_failed=false,
@resource_skipped=false,
@supports=[{:platform=>"unix"}, {:platform=>"windows"}]>,
:expectation_message=>"is expected to be reachable",
:waiver_data=>nil}
我们将非常感激地收到任何有关文档的帮助。
解决方案
如果没有 Kaom Te 在这里写的访问器方法https://stackoverflow.com/a/63623855/299774,那么instance_variable_get
像这样使用:
output[:resource_title].instance_variable_get(:@hostname)
但!当心!有人没有将此访问器作为公共方法提供这一事实意味着他们想要封装这些数据。
因此,如果您正在为某些生产系统执行此操作 - 在下一次 gem 更新后您会惊讶地发现它@hostname
不再存在(名称已更改),或者(更糟糕的是)它仍然存在但包含不同的数据。这是一个内部实现细节,可能会发生变化。
另一方面,如果这个对象来自一个开源项目——做一个 PR 并使其成为公共接口的一部分。
推荐阅读
- python-3.x - 如何使用 git 下载自动创建 docker 容器并使用 python 自动启动?
- python - 我可以在 python 中传递未定义的变量参数吗?
- swift - iOS firebase 通知在后台不起作用
- python - 如何在我的项目中插入 pytube 扩展
- python - 对包含 MySQL 数据库数据的 for 循环进行排序
- python - 如何读取多光谱图像?
- android - Flutter:提供者获取空值
- swiftui - SwiftUI mapkit 将区域设置为用户的当前位置
- javascript - AngularJS 日期选择器 CSS
- sql - dwh oracle中完整表与增量表与增量表之间的区别是什么