4
我有這個紅寶石腳本生成一個散列並將其保存到文件。元帥不能轉儲散列與默認proc(TypeError)
有時文件不存在或爲空,所以我總是先檢查它的存在。然後我將舊值加載到我的哈希中並嘗試再次保存。我現在一直在爲此苦苦掙扎。這是一個示例:
newAppName = ARGV[0]
newApp = Hash.new
newApp["url"] = ARGV[1]
newApp["ports"] = ARGV[2].to_i
apps = Hash.new { |h, k| h[k] = Hash.new }
# apps["test"] = {"url" => "www.test.com", "ports" => 3 }
appsFile = '/home/test/data/apps'
if File.exists?(appsFile)
apps = Marshal.load File.read(appsFile)
else
puts "Inserting first app into list..."
end
apps[newAppName] = newApp
serialisedApps = Marshal.dump(apps) # This line is where I get the error
File.open(appsFile, 'w') {|f| f.write(serialisedApps) }
現在我得到這個錯誤:
script.rb:53:in `dump': can't dump hash with default proc (TypeError)`
是什麼意思?我的散列錯了嗎?我如何解決它?
我試着用irb手動做它,它工作正常,雖然我在Mac上測試過,而且這個腳本在Linux中運行。他們不應該有所不同,對吧?
很好解釋 – Viren
我通過刪除所有「new {stuff}」代碼進行修復。作品完美無瑕。謝謝! – Apollo
感謝您解釋這個出色的神祕錯誤。 –