0
我有我要執行檢查,以確保查找找到了匹配的多維散列,但它似乎並沒有工作一樣單一維度哈希值。我的代碼適用於找到匹配的情況,但不適用於不匹配。我讀過帖子,在某些情況下,哈希會自動生成一個不匹配的密鑰,我認爲這會導致NIL,因爲我沒有專門設置一個值。該錯誤消息我得到一個不匹配的情況是:
can't convert Hash into String (TypeError)
鏈接到我的項目背景:代碼和超鏈接https://github.com/elvisimprsntr/siriproxy-redeye
摘錄來源:
# Channel number and command syntax to actual RedEye device commandIds
# Note: Must all be lower case. Use multiple entries for variability in Siri response.
@cmdId = Hash.new(&(p=lambda{|h,k| h[k] = Hash.new(&p)}))
@cmdId["all"]["cable box"]["0"] = "/commands/send?commandId=3"
@cmdId["all"]["cable box"]["zero"] = "/commands/send?commandId=3"
@cmdId["all"]["cable box"]["1"] = "/commands/send?commandId=4"
def send_command(command)
commandid = @cmdId[@reRoom][@reDevice][command.downcase.strip]
unless commandid.nil?
say "OK. Sending command #{command}."
# FIXIT: Does not properly handle no match. Results in "can't convert Hash into String (TypeError)"
# This may be due to the fact that dynamically created multidimensional hash will create new keys if a match is not found which will pass the NIL check.
Rest.get(@reIp[@reSel] + @roomId[@reRoom] + @deviceId[@reRoom][@reDevice] + commandid)
else
say "Sorry, I am not programmed for command #{command}."
end
request_completed
end
問題
如何定義/不同和/或測試初始化我的哈希值不匹配?
啊對..因爲你總是得到一個哈希,你可以安全地丟棄類型檢查:) – doesterr
太棒了!我用'除非commandid.empty?'或者我需要檢查兩者嗎? – Elvis
大聲笑我的回答你的評論是在你的問題上面:D(你可以編輯評論而不是刪除並重新發布ist) – doesterr