【发布时间】:2015-11-23 04:59:45
【问题描述】:
我有以下代码。我不明白为什么table['something'] 与table[:something] 不同。
require 'json'
data = '[{"type":"local","db":"all","user":"postgres","addr":null,"method":"ident"},{"type":"local","db":"all","user":"all","addr":null,"method":"ident"},{"type":"host","db":"all","user":"all","addr":"0.0.0.0/0","method":"md5"},{"type":"host","db":"all","user":"all","addr":"::1/128","method":"md5"},{"type":"local","db":"communicator","user":"communicator","addr":" ","method":"trust"}]'
table = JSON.parse(data)
table.each do | auth |
if (auth['user'])
print( '\n\n\n' + auth[:user] )
end
end
与print( '\n\n\n' + auth[:user] ) 在线,我收到一个错误:
TypeError: no implicit conversion of nil into String
from (irb):88:in `+'
这意味着通过:key 访问table 与'key' 不同。
为什么?如何转换 table 以使其与 :key 而不是 'key' 一起使用?
【问题讨论】:
-
如果您制作
downvote,请写下原因
标签: ruby