Ruby - undefined method push' for nil:NilClass (NoMethodError)



test = network_sg.properties为JSON。我想在JSONsecurityRules[]数组中添加一个哈希值。

我尝试test[:securityRules].push(new_rule),但我得到错误undefined method push' for nil:NilClass (NoMethodError)

你知道怎么解决吗?

NETWORK_SG = Azure::Armrest::Network::NetworkSecurityGroupService.new(conf)
network_sg = NETWORK_SG.get('testing_123', rg)
test = network_sg.properties
puts test
{
"provisioningState": "Succeeded",
"resourceGuid": "test",
"securityRules": [
{
"name": "SSH",
"id": "SSH",
"etag": "18",
"type": "Microsoft/securityRules",
"properties": {}
}
]
}

我想把下面的哈希值附加到securityRules[]数组。

new_rule = {
:name => 'rule_2',
:properties => {
:protocol          => 'TCP',
:sourceAddressPrefix => '*',
:destinationAddressPrefix => '*',
:access => 'Allow',
:destinationPortRange => '22',
:sourcePortRange => '*',
:priority => '301',
:direction => 'Inbound',
}
}
test[:securityRules].push(new_rule)

testRuby散列包含String键,而不是符号。如果你使用符号:securityRules作为键访问test,你会得到nil,因为这个键不存在。

使用字符串代替:

test["securityRules"].push(new_rule)

相关内容

  • 没有找到相关文章

最新更新