我正在嘗試將一些資料從 csv 檔案加載到我的程式中并收到以下錯誤訊息:undefined method `<<' for nil:NilClass error message
這是我的代碼:
def load_students
file = File.open("students.csv", "r")
file.readlines.each do | line |
name, cohort = line.chomp.split(",")
@students << ({:name => name, :cohort => cohort.to_sym})
end
file.close
end
@students指的是一個空陣列。
知道問題是什么嗎?
謝謝!
uj5u.com熱心網友回復:
@students指的是一個空陣列。
可以?(顯然不是,因為錯誤訊息說它是nil!!)您從未向我們展示過這個變數在哪里被初始化。
在 ruby?? 中nil,如果您沒有將它們定義為其他東西,則所有實體變數默認評估為。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440721.html
標籤:红宝石
上一篇:Ruby將逗號分隔值轉換為陣列
下一篇:看到“GettingErrno::ENOENT'nosuchfileordirectory@rb_sysopen'”時如何在Rails中獲取正確的Tempfile檔案路徑
