Ruby - NoMethodError: undefined method for 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"]

Here 是解释如何访问哈希值的一些很好的答案。