繁体   English   中英

Ruby - NoMethodError:hash 的未定义方法

[英]Ruby - NoMethodError: undefined method for hash

如何获取属性的值并将其设置为变量?

这是一个示例: 日志显示所有属性。 我想获取dialog_param_placement_availability_zone值并将其分配给一个变量。

$evm.root.attributes.sort.each { |k, v| log(:info, "\t Attribute: #{k} = #{v}")}


[----] I, [2021-12-08T07:42:45.138328 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: data_type = string
[----] I, [2021-12-08T07:42:45.139071 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_guest_access_key_pair = 5
[----] I, [2021-12-08T07:42:45.139928 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_instance_type = 26
[----] I, [2021-12-08T07:42:45.140745 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_list_ec2_cloudsubnets_ids =
[----] I, [2021-12-08T07:42:45.141400 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_list_ec2_template_guids = 60b75115-4f29-49fd-a8d5-d27364cfbef5
[----] I, [2021-12-08T07:42:45.142041 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_number_of_vms =
[----] I, [2021-12-08T07:42:45.142670 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_param_placement_availability_zone = 1
[----] I, [2021-12-08T07:42:45.143333 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: dialog_vm_name =
[----] I, [2021-12-08T07:42:45.144040 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: enable_rbac = false
[----] I, [2021-12-08T07:42:45.144976 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: miq_group = #<MiqAeMethodService::MiqAeServiceMiqGroup:0x0000565050716dc0>
[----] I, [2021-12-08T07:42:45.145857 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: miq_server = #<MiqAeMethodService::MiqAeServiceMiqServer:0x000056504ffed328>
[----] I, [2021-12-08T07:42:45.146521 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: miq_server_id = 1
[----] I, [2021-12-08T07:42:45.147148 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: object_name = list_ec2_cloudsubnets_ids
[----] I, [2021-12-08T07:42:45.147792 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: required = true
[----] I, [2021-12-08T07:42:45.148829 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: service_template = #<MiqAeMethodService::MiqAeServiceServiceTemplate:0x000056504beeddf0>
[----] I, [2021-12-08T07:42:45.149803 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: service_template_id = 6
[----] I, [2021-12-08T07:42:45.150583 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: sort_by = description
[----] I, [2021-12-08T07:42:45.151554 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: sort_order = ascending
[----] I, [2021-12-08T07:42:45.153210 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: tenant = #<MiqAeMethodService::MiqAeServiceTenant:0x000056504e1d0160>
[----] I, [2021-12-08T07:42:45.154263 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: user = #<MiqAeMethodService::MiqAeServiceUser:0x000056504e1d1470>
[----] I, [2021-12-08T07:42:45.154917 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: user_id = 1
[----] I, [2021-12-08T07:42:45.155546 #318:2b28283f1fa8]  INFO -- automation: <AEMethod list_ec2_cloudsubnets_ids>      Attribute: vmdb_object_type = service_template

我尝试了以下

availability_zone_id = $evm.root.attributes.dialog_param_placement_availability_zone`

但我得到了错误

undefined method dialog_param_placement_availability_zone' for #<Hash:0x00005650509e5808> (NoMethodError)`

您可以像这样访问值

availability_zone_id  = $evm.root.attributes["dialog_param_placement_availability_zone"]

以下是一些很好的答案,解释了如何访问 hash 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM