Ruby - NoMethodError:未定义的哈希方法
Posted
技术标签:
【中文标题】Ruby - NoMethodError:未定义的哈希方法【英文标题】:Ruby - NoMethodError: undefined method for hash 【发布时间】:2022-01-13 04:47:21 【问题描述】:如何获取属性的值并将其设置在变量中?
这是一个示例:日志显示所有属性。我想获取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)`
【问题讨论】:
你可以这样访问它$evm.root.attributes["dialog_param_placement_availability_zone"]
。
【参考方案1】:
你可以像这样访问值
availability_zone_id = $evm.root.attributes["dialog_param_placement_availability_zone"]
Here 是一些很好的答案,解释了如何访问哈希值。
【讨论】:
以上是关于Ruby - NoMethodError:未定义的哈希方法的主要内容,如果未能解决你的问题,请参考以下文章
Ruby on Rails - 未定义的方法`split'代表nil:StocksController中的NilClass / NoMethodError #search
RSpec NoMethodError:“主对象的未定义方法‘描述’”
Grunt contrib sass NoMethodError:未定义的方法`<
部署到Heroku:NoMethodError:nil:NilClass的未定义方法'+'
在Michael Hartl的Ruby on Rails教程中获取错误消息:MicropostsController中的NoMethodError #create,如何避免收到此消息?